docs: update decorator

This commit is contained in:
Ilkka Seppälä committed 2024-05-25 11:41:51 +03:00
1 parent b855476315
commit 410c0c69a3
1 file changed
+39 -38
+39 -38
View File
@@ -3,9 +3,10 @@ title: Decorator
category: Structural category: Structural
language: en language: en
tag: tag:
- Gang of Four
- Enhancement - Enhancement
- Extensibility - Extensibility
- Gang of Four
- Object composition
- Wrapping - Wrapping
--- ---
@@ -22,7 +23,7 @@ The Decorator pattern allows for the dynamic addition of responsibilities to obj
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. > Imagine a coffee shop where you can customize your coffee order. You start with a basic coffee, and you can add different ingredients like milk, sugar, whipped cream, and so on. Each addition is like a decorator in the Decorator design pattern. The base coffee object can be decorated with additional functionality (flavors, toppings) dynamically. For example, you can start with a plain coffee object, then wrap it with a milk decorator, followed by a sugar decorator, and finally a whipped cream decorator. Each decorator adds new features or modifies the behavior of the coffee object, similar to how the Decorator pattern works in software design.
In plain words In plain words
@@ -34,7 +35,9 @@ Wikipedia says
**Programmatic Example** **Programmatic Example**
Let's take the troll example. First of all we have a `SimpleTroll` implementing the `Troll` interface: 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.
First, we have a `SimpleTroll` implementing the `Troll` interface:
```java ```java
public interface Troll { public interface Troll {
@@ -68,16 +71,12 @@ public class SimpleTroll implements Troll {
Next, we want to add a 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
@RequiredArgsConstructor
public class ClubbedTroll implements Troll { public class ClubbedTroll implements Troll {
private final Troll decorated; private final Troll decorated;
public ClubbedTroll(Troll decorated) {
this.decorated = decorated;
}
@Override @Override
public void attack() { public void attack() {
decorated.attack(); decorated.attack();
@@ -99,39 +98,42 @@ public class ClubbedTroll implements Troll {
Here's the troll in action: Here's the troll in action:
```java ```java
// simple troll public static void main(String[] args) {
LOGGER.info("A simple looking troll approaches.");
var troll=new SimpleTroll();
troll.attack();
troll.fleeBattle();
LOGGER.info("Simple troll power: {}.\n",troll.getAttackPower());
// change the behavior of the simple troll by adding a decorator // simple troll
LOGGER.info("A troll with huge club surprises you."); LOGGER.info("A simple looking troll approaches.");
var clubbedTroll=new ClubbedTroll(troll); var troll = new SimpleTroll();
clubbedTroll.attack(); troll.attack();
clubbedTroll.fleeBattle(); troll.fleeBattle();
LOGGER.info("Clubbed troll power: {}.\n",clubbedTroll.getAttackPower()); LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
// 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);
clubbedTroll.attack();
clubbedTroll.fleeBattle();
LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());
}
``` ```
Program output: Program output:
```java ```
A simple looking troll approaches. 11:34:18.098 [main] INFO com.iluwatar.decorator.App -- A simple looking troll approaches.
The troll tries to grab you! 11:34:18.100 [main] INFO com.iluwatar.decorator.SimpleTroll -- The troll tries to grab you!
The troll shrieks in horror and runs away! 11:34:18.100 [main] INFO com.iluwatar.decorator.SimpleTroll -- The troll shrieks in horror and runs away!
Simple troll power:10. 11:34:18.100 [main] INFO com.iluwatar.decorator.App -- Simple troll power: 10.
A troll with huge club surprises you. 11:34:18.100 [main] INFO com.iluwatar.decorator.App -- A troll with huge club surprises you.
The troll tries to grab you! 11:34:18.101 [main] INFO com.iluwatar.decorator.SimpleTroll -- The troll tries to grab you!
The troll swings at you with a club! 11:34:18.101 [main] INFO com.iluwatar.decorator.ClubbedTroll -- The troll swings at you with a club!
The troll shrieks in horror and runs away! 11:34:18.101 [main] INFO com.iluwatar.decorator.SimpleTroll -- The troll shrieks in horror and runs away!
Clubbed troll power:20. 11:34:18.101 [main] INFO com.iluwatar.decorator.App -- Clubbed troll power: 20.
``` ```
## Class diagram ## Class diagram
![alt text](./etc/decorator.urm.png "Decorator pattern class diagram") ![Decorator](./etc/decorator.urm.png "Decorator pattern class diagram")
## Applicability ## Applicability
@@ -144,7 +146,7 @@ Decorator is used to:
## Tutorials ## Tutorials
* [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example) * [Decorator Design Pattern in Java Example (DigitalOcean)](https://www.digitalocean.com/community/tutorials/decorator-design-pattern-in-java-example)
## Known uses ## Known uses
@@ -177,10 +179,9 @@ Trade-offs:
## Credits ## Credits
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) * [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions](https://www.amazon.com/gp/product/1937785467/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1937785467&linkCode=as2&tag=javadesignpat-20&linkId=7e4e2fb7a141631491534255252fd08b) * [Functional Programming in Java](https://amzn.to/3JUIc5Q)
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31) * [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) * [J2EE Design Patterns](https://amzn.to/4dpzgmx)
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94)
* [Pattern-Oriented Software Architecture, Volume 1: A System of Patterns](https://amzn.to/4aKFTgS) * [Pattern-Oriented Software Architecture, Volume 1: A System of Patterns](https://amzn.to/4aKFTgS)
* [Refactoring to Patterns](https://amzn.to/3VOO4F5)