You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: StructuralPatterns/Decorator/README.md
+25-13Lines changed: 25 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,23 @@ Define `Decorator` objects that
13
13
* implement the interface of the extended (decorated) object (`Component`) transparently by forwarding all requests to it and
14
14
* perform additional functionality before/after forwarding a request.
15
15
16
+
## Common Structure
17
+
18
+

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
+
16
33
## Benefits
17
34
18
35
* provide a flexible alternative to subclassing for extending functionality.
@@ -22,6 +39,10 @@ Define `Decorator` objects that
22
39
23
40
* can result in many small objects and overuse can be complex.
24
41
* 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
25
46
26
47
See complete [Cake Factory](https://gitlab.com/tk-bachelor/se1-testat3-decorator) with Decorator Pattern in Java
***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.
0 commit comments