Decorator Pattern

Design Patterns (24 Part Series)

1 Main Design Patterns
2 Abstract Factory Pattern
20 more parts…
3 Adapter Pattern
4 Decorator Pattern
5 Strategy Pattern
6 Observer Pattern
7 Builder Pattern
8 Factory Method Pattern
9 Prototype Pattern
10 Singleton Pattern
11 Bridge Pattern
12 Composite Pattern
13 Facade Pattern
14 Flyweight Pattern
15 Proxy Pattern
16 Chain of Responsibility Pattern
17 Command Pattern
18 Interpreter Pattern
19 Iterator Pattern
20 Mediator Pattern
21 Memento Pattern
22 State Pattern
23 Template Method Pattern
24 Visitor Pattern

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

Participants

  • Component: defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent: defines an object to which additional responsibilities can be attached.
  • Decorator: maintains a reference to a Component object and defines an interface that conforms to Component’s interface.
  • ConcreteDecorator: adds responsibilities to the component.

Code

public class Main {

  public static void main(String[] args) {
    ConcreteComponent c = new ConcreteComponent();
    ConcreteDecoratorA d1 = new ConcreteDecoratorA();
    ConcreteDecoratorB d2 = new ConcreteDecoratorB();
    d1.setComponent(c);
    d2.setComponent(d1);
    d2.operation();
  }
}

public interface Component {
  void operation();
}

public class ConcreteComponent implements Component {

  @Override
  public void operation() {
    System.out.println("ConcreteComponent.operation()");
  }
}

public abstract class Decorator implements Component {

  protected Component component;

  public void setComponent(Component component) {
    this.component = component;
  }

  @Override
  public void operation() {
    if (component != null) {
      component.operation();
    }
  }
}

public class ConcreteDecoratorA extends Decorator {

  private static final String ADDED_STATE = "ConcreteDecoratorA.operation()";

  @Override
  public void operation() {
    super.operation();
    System.out.println(ADDED_STATE);
  }

}

public class ConcreteDecoratorB extends Decorator {

  @Override
  public void operation() {
    super.operation();
    addedBehavior();
    System.out.println("ConcreteDecoratorB.operation()");
  }

  private void addedBehavior() {
    System.out.println("ConcreteDecoratorB.addedBehavior()");
  }
}

Enter fullscreen mode Exit fullscreen mode

Output

ConcreteComponent.operation()
ConcreteDecoratorA.operation()
ConcreteDecoratorB.addedBehavior()
ConcreteDecoratorB.operation()

Enter fullscreen mode Exit fullscreen mode

eidherjulian61 / design-patterns


图片[1]-Decorator Pattern - 拾光赋-拾光赋

Main Design Patterns

eidher ・ Sep 27 ’20 ・ 1 min read

#designpatterns #creational #structural #behavioral

Design Patterns (24 Part Series)

1 Main Design Patterns
2 Abstract Factory Pattern
20 more parts…
3 Adapter Pattern
4 Decorator Pattern
5 Strategy Pattern
6 Observer Pattern
7 Builder Pattern
8 Factory Method Pattern
9 Prototype Pattern
10 Singleton Pattern
11 Bridge Pattern
12 Composite Pattern
13 Facade Pattern
14 Flyweight Pattern
15 Proxy Pattern
16 Chain of Responsibility Pattern
17 Command Pattern
18 Interpreter Pattern
19 Iterator Pattern
20 Mediator Pattern
21 Memento Pattern
22 State Pattern
23 Template Method Pattern
24 Visitor Pattern

原文链接:Decorator Pattern

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容