Skip to content

Commit e4e29a3

Browse files
committed
complete Decorator pattern
1 parent 170be3a commit e4e29a3

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

StructuralPatterns/Decorator/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ Define `Decorator` objects that
1313
* implement the interface of the extended (decorated) object (`Component`) transparently by forwarding all requests to it and
1414
* perform additional functionality before/after forwarding a request.
1515

16+
## Common Structure
17+
18+
![Common structure of decorator pattern](img/structure.jpg)
19+
20+
* Component (Cake)
21+
* defines the interface for objects that can have responsibilities added to them dynamically.
22+
* ConcreteComponent (MuffinCake)
23+
* defines an object to which additional responsibilities can be attached.
24+
* Decorator (CakeDecorator)
25+
* maintains a reference to a Component object and defines an interface that conforms to Component's interface.
26+
* ConcreteDecorator (WhippedCream)
27+
* adds responsibilities to the component.
28+
29+
## Collaboration
30+
31+
Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.
32+
1633
## Benefits
1734

1835
* provide a flexible alternative to subclassing for extending functionality.
@@ -22,6 +39,10 @@ Define `Decorator` objects that
2239

2340
* can result in many small objects and overuse can be complex.
2441
* can cause issues if the client relies heavily on the components concrete type.
42+
* A decorator and its component aren't identical.
43+
* A decorator acts a transparant enclosure. From an object identity point of view, a decorated component is not identical to the component itself. Hence you shouldn't rely on object identity when you use decorators.
44+
45+
## Example
2546

2647
See complete [Cake Factory](https://gitlab.com/tk-bachelor/se1-testat3-decorator) with Decorator Pattern in Java
2748

@@ -141,17 +162,8 @@ assertEquals("muffin cake description",
141162
assertEquals(cake.getDescription(), 34, cake.cost(), 0);
142163
```
143164

144-
## Common Structure
145-
146-
![Common structure of decorator pattern](http://www.dofactory.com/images/diagrams/net/decorator.gif)
147-
148-
* Component (Cake)
149-
* defines the interface for objects that can have responsibilities added to them dynamically.
150-
* ConcreteComponent (MuffinCake)
151-
* defines an object to which additional responsibilities can be attached.
152-
* Decorator (CakeDecorator)
153-
* maintains a reference to a Component object and defines an interface that conforms to Component's interface.
154-
* ConcreteDecorator (WhippedCream)
155-
* adds responsibilities to the component.
165+
## Relations with Other Patterns
156166

157-
_[Source: http://www.dofactory.com/net/decorator-design-pattern]_
167+
* **Adapter**: A decorator is different from an adapter in that a decorator only changes an object's responsibilities, not its interface; an adapter will give an object a completely new interface.
168+
* **Composite**: A decorator can be viewed as a degenerate composite with **only one component**. However, a decorator adds additional responsibilities - it isn't intended for object aggregation.
169+
* **Strategy**: A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways of changing an object.
38.1 KB
Loading
49.5 KB
Loading

0 commit comments

Comments
 (0)