0

I have a question regarding on how can I make a diagram by using my code.

I have a class Player and inside of this class there are some methods. I have another class called HealthManager which decides how much health would the player have.

In my Playerclass I'm using a HealthManager object to make some decisions (like dropping health to 0) and in my HealthManager class I'm using a PlayerClass object for some stats.

Can I call this type of relation an Association ?

  • 1
    In case of *association* it´s possible that `Player` instance can have *zero* `HealthManager` instances. Is this OK? It's also possible that `Player` instance can have *many* (say, five) `HealthManager` instances. Is that OK? If *both* answers are yes, then it's an association. I, personally, guess that it should be 1:1 relation (each `Player` has one and only one `HealthManager`) – Dmitry Bychenko May 24 '16 at 08:28
  • Two questions: (1) Suppose Player object X uses HealthManager object Y. Will Y then use only X for stats, or could it use a different Player object Z ? (2) Do the objects have a lifetime dependency, i.e. will the HealthManager object be destroyed if the associated Player object is destroyed or vice versa? – www.admiraalit.nl May 24 '16 at 09:29
  • For the first question: Yes It would be a 1:1 relation , each player has one and only one HealthManager. For the second question, I didn't do any object destruction at all, when my player dies, it will be revived in one spot. So is the death thing actually be an object destruction, because in my code I didn't delete my object – user3626136 May 24 '16 at 10:48
  • I was wondering whether the relationship would be a composition, which is a specific form of association. This type of association is used if one object is regarded to be a part of the other object. If the latter is destroyed, its parts are obviously also destroyed. I think a composition is not applicable in your case. – www.admiraalit.nl May 24 '16 at 11:50
  • This sounds like a cyclic relationship; that is, `Player` depends on `HealthManager` which depends on `Player` again for stats. Is the relationship conditional, i.e. is the relationship established only under certain conditions, such as when a particular method is called? If yes, how is the relationship established? Is a `HealthManager` passed as an argument in a call to a method of `Player`? If the relationship is not conditional, it is an association, which can in turn be a composition or an aggregation, depending on how the lifetime of the related object is controlled. –  May 24 '16 at 16:07
  • So in my PlayerClass I'm checking if player is hurt with if condition and if he's hurt than a red animation will play on the screen. In my HealthManager I check if Health object is 0 -> LevelManager.Respawn() (Level Manager is another class which respawns my player) – user3626136 May 24 '16 at 16:40

2 Answers2

0

Yes, you can call it a 1:1 association.

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
0

It's a question if the HealthManager is a part of Player and it seems so according to your description (and comments).

This means you have here a whole-part relationship, moreover HealthManager is assigned to exactly one Player which means it's a composite aggregation (composition). The fact that HealthManager can retrieve some information from the Player shows only that this composition has also backward navigability (i.e. from HealthManager to Player) which is absolutely fine (and is depicted with an open arrow just before the filled diamond).

If the HealthManager is destroyed when it's Player is destroyed then you're almost sure that it's a composition.

Consider also that both shared and composite aggregations are specialisations of association i.e. they are also associations. Having this said if you just document a 1:1 association instead of composition it's still correct, just don't give some details about your model.

Ister
  • 5,958
  • 17
  • 26