Minor updates to decorator pattern
This commit is contained in:
parent
abc8e0d88a
commit
57783aa341
@ -6,7 +6,7 @@ permalink: /patterns/decorator/
|
|||||||
categories: Structural
|
categories: Structural
|
||||||
language: en
|
language: en
|
||||||
tags:
|
tags:
|
||||||
- Gang Of Four
|
- Gang of Four
|
||||||
- Extensibility
|
- Extensibility
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -21,9 +21,9 @@ 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
|
> 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
|
> 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.
|
> dynamically with a suitable weapon.
|
||||||
|
|
||||||
@ -72,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 a club for the troll. We can do it dynamically by using a decorator:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -106,23 +106,33 @@ Here's the troll in action:
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
// simple troll
|
// simple troll
|
||||||
|
LOGGER.info("A simple looking troll approaches.");
|
||||||
var troll = new SimpleTroll();
|
var troll = new SimpleTroll();
|
||||||
troll.attack(); // The troll tries to grab you!
|
troll.attack();
|
||||||
troll.fleeBattle(); // The troll shrieks in horror and runs away!
|
troll.fleeBattle();
|
||||||
|
LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
|
||||||
|
|
||||||
// change the behavior of the simple troll by adding a decorator
|
// change the behavior of the simple troll by adding a decorator
|
||||||
|
LOGGER.info("A troll with huge club surprises you.");
|
||||||
var clubbedTroll = new ClubbedTroll(troll);
|
var clubbedTroll = new ClubbedTroll(troll);
|
||||||
clubbedTroll.attack(); // The troll tries to grab you! The troll swings at you with a club!
|
clubbedTroll.attack();
|
||||||
clubbedTroll.fleeBattle(); // The troll shrieks in horror and runs away!
|
clubbedTroll.fleeBattle();
|
||||||
|
LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output:
|
Program output:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
A simple looking troll approaches.
|
||||||
The troll tries to grab you!
|
The troll tries to grab you!
|
||||||
The troll shrieks in horror and runs away!
|
The troll shrieks in horror and runs away!
|
||||||
The troll tries to grab you! The troll swings at you with a club!
|
Simple troll power: 10.
|
||||||
|
|
||||||
|
A troll with huge club surprises you.
|
||||||
|
The troll tries to grab you!
|
||||||
|
The troll swings at you with a club!
|
||||||
The troll shrieks in horror and runs away!
|
The troll shrieks in horror and runs away!
|
||||||
|
Clubbed troll power: 20.
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
@ -140,11 +150,11 @@ affecting other objects.
|
|||||||
are possible and would produce an explosion of subclasses to support every combination. Or a class
|
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.
|
definition may be hidden or otherwise unavailable for subclassing.
|
||||||
|
|
||||||
## Tutorial
|
## Tutorials
|
||||||
|
|
||||||
* [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
|
## Known uses
|
||||||
|
|
||||||
* [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)
|
||||||
|
@ -50,13 +50,13 @@ public class App {
|
|||||||
var troll = new SimpleTroll();
|
var troll = new SimpleTroll();
|
||||||
troll.attack();
|
troll.attack();
|
||||||
troll.fleeBattle();
|
troll.fleeBattle();
|
||||||
LOGGER.info("Simple troll power {}.\n", troll.getAttackPower());
|
LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
|
||||||
|
|
||||||
// change the behavior of the simple troll by adding a decorator
|
// change the behavior of the simple troll by adding a decorator
|
||||||
LOGGER.info("A troll with huge club surprises you.");
|
LOGGER.info("A troll with huge club surprises you.");
|
||||||
var clubbedTroll = new ClubbedTroll(troll);
|
var clubbedTroll = new ClubbedTroll(troll);
|
||||||
clubbedTroll.attack();
|
clubbedTroll.attack();
|
||||||
clubbedTroll.fleeBattle();
|
clubbedTroll.fleeBattle();
|
||||||
LOGGER.info("Clubbed troll power {}.\n", clubbedTroll.getAttackPower());
|
LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user