How do you represent a generalization that can contain instances of itself or its parent in UML?
For example, consider 开发者_如何转开发the following: There is a class "Text Group" which is a generalization of class "Text Object." Text Groups can contain one or more Text Objects or one or more Text Groups. How would I represent this in UML? Any relevant links?
Thanks!
Think you want the Composite Pattern.
hth.
I think that u just composite it like it was any other class.
Generalisation shows the relation in inheritance tree between the parent and child classes. If you want to show containment, use composition or aggreagation relationship. As mentioned above, Composite design pattern is a perfect fit for your problem. You use compostion with an interface, which you implement with both the node and leaf classes in your composition hierarchy.
Classes are not restricted to a single relationship, and can be self referencing. You can draw both a generalisation and a composition relationship.
However I would question your specification of "one or more" multiplicity in a self referencing class since that will require either a circular or infinitely recursive reference. "zero or more" would probably be more appropriate. I would also question the semantics of a single object being a type of group, sounds counter-intuitive perhaps.
精彩评论