Skip to content

Commit 2dd2cfb

Browse files
committed
Update README.md
1 parent 8512c65 commit 2dd2cfb

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

decorator/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,39 @@ tags:
1010
---
1111

1212
## Also known as
13+
1314
Wrapper
1415

1516
## 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.
1920

2021
## Explanation
2122

2223
Real world example
2324

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.
2528
2629
In plain words
2730

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.
2933
3034
Wikipedia says
3135

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.
3341
3442
**Programmatic Example**
3543

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:
3746

3847
```java
3948
public interface Troll {
@@ -63,7 +72,7 @@ public class SimpleTroll implements Troll {
6372
}
6473
```
6574

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:
6776

6877
```java
6978
public class ClubbedTroll implements Troll {
@@ -94,7 +103,7 @@ public class ClubbedTroll implements Troll {
94103
}
95104
```
96105

97-
Here's the troll in action
106+
Here's the troll in action:
98107

99108
```java
100109
// simple troll
@@ -108,20 +117,36 @@ clubbedTroll.attack(); // The troll tries to grab you! The troll swings at you w
108117
clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away!
109118
```
110119

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+
111129
## Class diagram
130+
112131
![alt text](./etc/decorator.urm.png "Decorator pattern class diagram")
113132

114133
## Applicability
115-
Use Decorator
116134

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.
120143

121144
## Tutorial
145+
122146
* [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example)
123147

124148
## Real world examples
149+
125150
* [java.io.InputStream](http://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html), [java.io.OutputStream](http://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html),
126151
[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)
127152
* [java.util.Collections#synchronizedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedCollection-java.util.Collection-)

0 commit comments

Comments
 (0)