Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 889
noob.spt Avatar asked Nov 20 '09 22:11

noob.spt


People also ask

What is Decorator in design patterns?

The decorator design pattern is a structural pattern, which provides a wrapper to the existing class. The decorator design pattern uses abstract classes or interfaces with the composition to implement the wrapper.

Where do we use Decorator design pattern explain with example?

Example. The Decorator attaches additional responsibilities to an object dynamically. The ornaments that are added to pine or fir trees are examples of Decorators. Lights, garland, candy canes, glass ornaments, etc., can be added to a tree to give it a festive look.

What are the reasons for using the decorator design pattern?

The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new Decorator class that wraps the original class.

What are two capabilities of the decorator pattern?

Here are some of the properties of decorators: Decorators have the same super type as the object they decorate. You can use multiple decorators to wrap an object. Since decorators have same type as object, we can pass around decorated object instead of original.


2 Answers

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.

like image 159
Adam Byrtek Avatar answered Oct 04 '22 19:10

Adam Byrtek


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.

like image 35
FinnNk Avatar answered Oct 04 '22 21:10

FinnNk