Browser docs

Decorator

Also known as

Wrapper

Intent

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Explanation

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.

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.

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 as well as to the Open-Closed Principle, by allowing the functionality of a class to be extended without being modified.

Programmatic Example

Let’s take the troll example. First of all we have a SimpleTroll implementing the Troll interface:

 1public interface Troll {
 2  void attack();
 3  int getAttackPower();
 4  void fleeBattle();
 5}
 6
 7@Slf4j
 8public class SimpleTroll implements Troll {
 9
10  @Override
11  public void attack() {
12    LOGGER.info("The troll tries to grab you!");
13  }
14
15  @Override
16  public int getAttackPower() {
17    return 10;
18  }
19
20  @Override
21  public void fleeBattle() {
22    LOGGER.info("The troll shrieks in horror and runs away!");
23  }
24}

Next, we want to add a club for the troll. We can do it dynamically by using a decorator:

 1@Slf4j
 2public class ClubbedTroll implements Troll {
 3
 4  private final Troll decorated;
 5
 6  public ClubbedTroll(Troll decorated) {
 7    this.decorated = decorated;
 8  }
 9
10  @Override
11  public void attack() {
12    decorated.attack();
13    LOGGER.info("The troll swings at you with a club!");
14  }
15
16  @Override
17  public int getAttackPower() {
18    return decorated.getAttackPower() + 10;
19  }
20
21  @Override
22  public void fleeBattle() {
23    decorated.fleeBattle();
24  }
25}

Here’s the troll in action:

 1// simple troll
 2LOGGER.info("A simple looking troll approaches.");
 3var troll = new SimpleTroll();
 4troll.attack();
 5troll.fleeBattle();
 6LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
 7
 8// change the behavior of the simple troll by adding a decorator
 9LOGGER.info("A troll with huge club surprises you.");
10var clubbedTroll = new ClubbedTroll(troll);
11clubbedTroll.attack();
12clubbedTroll.fleeBattle();
13LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());

Program output:

 1A simple looking troll approaches.
 2The troll tries to grab you!
 3The troll shrieks in horror and runs away!
 4Simple troll power: 10.
 5
 6A troll with huge club surprises you.
 7The troll tries to grab you!
 8The troll swings at you with a club!
 9The troll shrieks in horror and runs away!
10Clubbed troll power: 20.

Class diagram

alt text

Applicability

Decorator is used to:

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

Tutorials

Known uses

Credits