Is it possible and does it make sense that Hero has an aggregation and composition relationship?
My aim is to keep a list of powers for each hero, however when creating one it should only be possible to add one Power instead of multiple Powers. That is the reason why I've created a private attribute power for only one power.
My question is about the validity of the UML class diagram. Is that the correct way of displaying my intend?
The reason why power is an aggregation is that it could be that another class uses the same power instance (I have implemented some cache which always return the same instance of a power, for simplicity I didn't show that part).
The reason why powers is an composition is that it strongly depends on the hero instance. If hero is destroyed, powers should also be cleaned.
The code for the UML does look like something like that (TypeScript):
hero.ts
import { Power } from './power';
export class Hero {
powers: Power[] = [];
constructor(private power?: Power) {
if (power) {
this.addPower(power);
}
}
addPower(power: Power) {
this.powers.push(power);
}
}
power.ts
export class Power {
constructor(private title: string) { }
}