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
{{ message }}
Repository navigation
Commit 9d2f0c6
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: decorator/README.md
+90-1Lines changed: 90 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,96 @@ Attach additional responsibilities to an object dynamically.
19
19
Decorators provide a flexible alternative to subclassing for extending
20
20
functionality.
21
21
22
-

22
+
## Explanation
23
+
24
+
Real world example
25
+
26
+
> There is an angry troll living in the nearby hills. Usually it goes bare handed but sometimes it has a weapon. To arm the troll it's not necessary to create a new troll but to decorate it dynamically with a suitable weapon.
27
+
28
+
In plain words
29
+
30
+
> Decorator pattern lets you dynamically change the behavior of an object at run time by wrapping them in an object of a decorator class.
31
+
32
+
Wikipedia says
33
+
34
+
> In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern.
35
+
36
+
**Programmatic Example**
37
+
38
+
Lets take the troll example. First of all we have a simple troll implementing the troll interface
39
+
40
+
```
41
+
public interface Troll {
42
+
void attack();
43
+
int getAttackPower();
44
+
void fleeBattle();
45
+
}
46
+
47
+
public class SimpleTroll implements Troll {
48
+
49
+
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTroll.class);
50
+
51
+
@Override
52
+
public void attack() {
53
+
LOGGER.info("The troll tries to grab you!");
54
+
}
55
+
56
+
@Override
57
+
public int getAttackPower() {
58
+
return 10;
59
+
}
60
+
61
+
@Override
62
+
public void fleeBattle() {
63
+
LOGGER.info("The troll shrieks in horror and runs away!");
64
+
}
65
+
}
66
+
```
67
+
68
+
Next we want to add club for the troll. We can do it dynamically by using a decorator
69
+
70
+
```
71
+
public class ClubbedTroll implements Troll {
72
+
73
+
private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class);
74
+
75
+
private Troll decorated;
76
+
77
+
public ClubbedTroll(Troll decorated) {
78
+
this.decorated = decorated;
79
+
}
80
+
81
+
@Override
82
+
public void attack() {
83
+
decorated.attack();
84
+
LOGGER.info("The troll swings at you with a club!");
85
+
}
86
+
87
+
@Override
88
+
public int getAttackPower() {
89
+
return decorated.getAttackPower() + 10;
90
+
}
91
+
92
+
@Override
93
+
public void fleeBattle() {
94
+
decorated.fleeBattle();
95
+
}
96
+
}
97
+
```
98
+
99
+
Here's the troll in action
100
+
101
+
```
102
+
// simple troll
103
+
Troll troll = new SimpleTroll();
104
+
troll.attack(); // The troll tries to grab you!
105
+
troll.fleeBattle(); // The troll shrieks in horror and runs away!
106
+
107
+
// change the behavior of the simple troll by adding a decorator
108
+
Troll clubbed = new ClubbedTroll(troll);
109
+
clubbed.attack(); // The troll tries to grab you! The troll swings at you with a club!
110
+
clubbed.fleeBattle(); // The troll shrieks in horror and runs away!
0 commit comments