English 中文(简体)
Understanding UML of DoFactory Design Pattern - Decorator
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

Design pattern for managing queues and stacks?

Is there a design pattern for managing a queue or a stack? For example, we are looking to manage a list of tasks. These tasks will be added to a group queue, users will then be able to pull off the ...

Organizing classes using the repository design pattern

I have started upgrading one of our internal software applications, written in ASP.NET Web Forms, and moving to ASP.NET MVC. I am trying to leverage the Repository design pattern for my classes, ...

Misuse of Observer Pattern?

I have a Car object which contains a latitude field and a longitude field. I use the observer pattern so that any time either of these fields change in my application, my car object is notified. I ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...

热门标签