How to modelling different types of fields and entities connection in UML

落爺英雄遲暮 提交于 2021-01-28 06:06:28

问题


The task:

  • Each field has place for multiple entities and have 1-10 neighbors. The fields are different. Some of them are trapped and if an entity steps on them they will not be able to leave. There are also radioactive fields and if an entity spends too much time on them, they dies. Fields can not be trapped and radioactive at the same time.
  • There are two different entities. Some of them are able to escape from trapped fields (E1) and some of them are immune for radioactivity (E2). Entities can move between neighboring tiles.

From this I have got the following class diagram (only the classes):

So I want to know how can I represent that if E1 steps on Radioactive and if it stays there too long dies? Furthermore how can it handle that case when a new E1 comes and it does not die when the first one?

First I tried with something like this, but it can not handle that event

EDIT: My soloutin:


回答1:


First diagram (first version on the question)

Your generalizations if they exist are in the wrong direction, currently a Field is a Trapped and a Radioactive and an Entity is a E1 and a E2, but you wanted the reverse.

Anyway there is nothing in the statement saying a field cannot be both trapped and radioactive, in that case rather than to use generalizations better to use properties.

For the Entities it seems there are only E1 and E2 so you can use generalization (in reverse direction)

Second diagram

Note if Radioactive implements a Timer that means someone else has to kick each Radioactive and only them (no other Field).

Concerning the time to die, if it is the same whatever the instance of E1 and Radioactive that attribute can be static.

I do not understand the operation KillEntity because the moment an E1 is killed depends on when each instance enter in the field so cannot be general

You can handle the delay to die for a Field in a Radioactive in that least, for instance :

  • each time an Entity enter in a Radioactive the Entity is added in dying for instance initializing duration to 0
  • each time an Entity exit in a Radioactive the Entity is removed in dying
  • each time tick is called each duration is increased and when becoming timeToDie the corresponding Entity is removed from dying and the operation die called on it
  • the operation dies does noting on Entity but on E2 indicates the instance is killed (it is also possible to have that operation abstract on Entity and defined but doing nothing on E1)

Note you can replace the class-association by an association with a qualifier valuing the duration etc

But you can also handle the delay to die in E2 :

  • tick on Entity/E2 is applied by a Radioactive on its Fields each time it is itself ticked, tick does nothing on Entity but manages the duration on E2 to kill the instance after the required delay (a static attribute can be shown again)

  • each time an E2 moves to a new Field the duration is reset to 0


Note your multiplicity 1..* is wrong because a Field may not have Entity, on the opposite side the multiplicity is 1 if a dead E2 stay in a Radioactive else 0..1



来源:https://stackoverflow.com/questions/64946909/how-to-modelling-different-types-of-fields-and-entities-connection-in-uml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!