Understanding UML of DoFactory Design Pattern - Decorator

倖福魔咒の 提交于 2019-12-10 05:47:56

问题


I am trying to understand UML diagram describing Decorator Pattern at link below

http://www.dofactory.com/Patterns/PatternDecorator.aspx

I don't understand why there is a "Aggregation" relation between Decorator and Component.

I believe it should be composition as Decorator cannot exist without the base component.


回答1:


Composition is stronger that aggregation, it usually means that the object takes ownership of its components. This is not the case in this situation because a decorator doesn't own a decorated object. Moreover you could remove the decorator without a need to remove the decorated object as well.

In practice the line between aggregation and composition can be blurry and often it doesn't make much difference whether you choose one or the other, especially if you treat you diagrams as sketches.




回答2:


Basically because you can have multiple decorators on the component. From wikipedia on aggregation:

Differences between Composition and Aggregation

The whole of a composition must have a multiplicity of 0..1 or 1, indicating that a part must be for only one whole. The whole of an aggregation may have any multiplicity.

also

Composition usually has a strong life cycle dependency between instances of the container class and instances of the contained class(es): If the container is destroyed, normally every instance that it contains is destroyed as well.

note the use of the word 'usually'.

Take a look at the example decorator diagram, also at wikipedia for a clearer example of why this is the case.



来源:https://stackoverflow.com/questions/1773605/understanding-uml-of-dofactory-design-pattern-decorator

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