Update README.md

This commit is contained in:
Ilkka Seppälä 2020-08-29 16:49:45 +03:00
parent 8512c65aef
commit 2dd2cfb8ca

View File

@ -10,30 +10,39 @@ tags:
--- ---
## Also known as ## Also known as
Wrapper Wrapper
## Intent ## Intent
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending Attach additional responsibilities to an object dynamically. Decorators provide a flexible
functionality. alternative to subclassing for extending functionality.
## Explanation ## Explanation
Real world example Real world example
> 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. > 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.
In plain words In plain words
> Decorator pattern lets you dynamically change the behavior of an object at run time by wrapping them in an object of a decorator class. > Decorator pattern lets you dynamically change the behavior of an object at run time by wrapping
> them in an object of a decorator class.
Wikipedia says Wikipedia says
> 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. > 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.
**Programmatic Example** **Programmatic Example**
Let's take the troll example. First of all we have a simple troll implementing the troll interface Let's take the troll example. First of all we have a `SimpleTroll` implementing the `Troll`
interface:
```java ```java
public interface Troll { public interface Troll {
@ -63,7 +72,7 @@ public class SimpleTroll implements Troll {
} }
``` ```
Next we want to add club for the troll. We can do it dynamically by using a decorator Next we want to add club for the troll. We can do it dynamically by using a decorator:
```java ```java
public class ClubbedTroll implements Troll { public class ClubbedTroll implements Troll {
@ -94,7 +103,7 @@ public class ClubbedTroll implements Troll {
} }
``` ```
Here's the troll in action Here's the troll in action:
```java ```java
// simple troll // simple troll
@ -108,20 +117,36 @@ clubbedTroll.attack(); // The troll tries to grab you! The troll swings at you w
clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away! clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away!
``` ```
Program output:
```java
The troll tries to grab you!
The troll shrieks in horror and runs away!
The troll tries to grab you! The troll swings at you with a club!
The troll shrieks in horror and runs away!
```
## Class diagram ## Class diagram
![alt text](./etc/decorator.urm.png "Decorator pattern class diagram") ![alt text](./etc/decorator.urm.png "Decorator pattern class diagram")
## Applicability ## Applicability
Use Decorator
* To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects Decorator is used to:
* For responsibilities that can be withdrawn
* 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 * Add responsibilities to individual objects dynamically and transparently, that is, without
affecting other objects.
* For responsibilities that can be withdrawn.
* 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.
## Tutorial ## Tutorial
* [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example) * [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example)
## Real world examples ## Real world examples
* [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), * [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),
[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) [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)
* [java.util.Collections#synchronizedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedCollection-java.util.Collection-) * [java.util.Collections#synchronizedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedCollection-java.util.Collection-)