开发者

UML - association or aggregation relationship?

When a class holds a type of another class as an attribute, what kind of relationship is this - association or aggregation?

e.g.

a Flight class holding a type of City class as an attribute called Destination - is this an association or aggregation? What would be the name of their relationship in either case? Flight- "arriving" -City?

Another scenario:

A FlightBooking class holding a type of BusinessClass class as an attribute to show that it is a business class flightbooking - is this association or ag开发者_高级运维gregation?

Thanks!


A Flight class holding a type of City class as an attribute called Destination - is this an association or aggregation?

That depends on what kind of behaviour you want to get in your program, but in most cases, it should be an association. When you delete the Flight object (in memory, in your database etc.), you don't want the destination City data to be deleted too, or do you? As a general rule of thumb: use an aggregation of B in A whenever you want the lifetime of A and B to be the same; use an association whenever the lifetime should be decoupled.


Think of the terminology - association is much more general-sounding. It makes sense to say that for "a Flight class holding a type of City class as an attribute called Destination" - that this is an association.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜