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: decorator/README.md
+38-13Lines changed: 38 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,30 +10,39 @@ tags:
10
10
---
11
11
12
12
## Also known as
13
+
13
14
Wrapper
14
15
15
16
## Intent
16
-
Attach additional responsibilities to an object dynamically.
17
-
Decorators provide a flexible alternative to subclassing for extending
18
-
functionality.
17
+
18
+
Attach additional responsibilities to an object dynamically. Decorators provide a flexible
19
+
alternative to subclassing for extending functionality.
19
20
20
21
## Explanation
21
22
22
23
Real world example
23
24
24
-
> 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.
25
+
> There is an angry troll living in the nearby hills. Usually it goes bare handed but sometimes it
26
+
> has a weapon. To arm the troll it's not necessary to create a new troll but to decorate it
27
+
> dynamically with a suitable weapon.
25
28
26
29
In plain words
27
30
28
-
> 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
+
> Decorator pattern lets you dynamically change the behavior of an object at run time by wrapping
32
+
> them in an object of a decorator class.
29
33
30
34
Wikipedia says
31
35
32
-
> 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.
36
+
> In object-oriented programming, the decorator pattern is a design pattern that allows behavior to
37
+
> be added to an individual object, either statically or dynamically, without affecting the behavior
38
+
> of other objects from the same class. The decorator pattern is often useful for adhering to the
39
+
> Single Responsibility Principle, as it allows functionality to be divided between classes with
40
+
> unique areas of concern.
33
41
34
42
**Programmatic Example**
35
43
36
-
Let's take the troll example. First of all we have a simple troll implementing the troll interface
44
+
Let's take the troll example. First of all we have a `SimpleTroll` implementing the `Troll`
45
+
interface:
37
46
38
47
```java
39
48
publicinterfaceTroll {
@@ -63,7 +72,7 @@ public class SimpleTroll implements Troll {
63
72
}
64
73
```
65
74
66
-
Next we want to add club for the troll. We can do it dynamically by using a decorator
75
+
Next we want to add club for the troll. We can do it dynamically by using a decorator:
67
76
68
77
```java
69
78
publicclassClubbedTrollimplementsTroll {
@@ -94,7 +103,7 @@ public class ClubbedTroll implements Troll {
94
103
}
95
104
```
96
105
97
-
Here's the troll in action
106
+
Here's the troll in action:
98
107
99
108
```java
100
109
// simple troll
@@ -108,20 +117,36 @@ clubbedTroll.attack(); // The troll tries to grab you! The troll swings at you w
108
117
clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away!
109
118
```
110
119
120
+
Program output:
121
+
122
+
```java
123
+
The troll tries to grab you!
124
+
The troll shrieks in horror and runs away!
125
+
The troll tries to grab you!The troll swings at you with a club!
126
+
The troll shrieks in horror and runs away!
127
+
```
128
+
111
129
## Class diagram
130
+
112
131

113
132
114
133
## Applicability
115
-
Use Decorator
116
134
117
-
* To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects
118
-
* For responsibilities that can be withdrawn
119
-
* When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing
135
+
Decorator is used to:
136
+
137
+
* Add responsibilities to individual objects dynamically and transparently, that is, without
138
+
affecting other objects.
139
+
* For responsibilities that can be withdrawn.
140
+
* When extension by subclassing is impractical. Sometimes a large number of independent extensions
141
+
are possible and would produce an explosion of subclasses to support every combination. Or a class
142
+
definition may be hidden or otherwise unavailable for subclassing.
[java.io.Reader](http://docs.oracle.com/javase/8/docs/api/java/io/Reader.html) and [java.io.Writer](http://docs.oracle.com/javase/8/docs/api/java/io/Writer.html)
0 commit comments