Home  >  Article  >  Java  >  A preliminary study on Java decorator design patterns

A preliminary study on Java decorator design patterns

高洛峰
高洛峰Original
2017-01-19 16:09:301160browse

This essay mainly introduces the implementation of simple decorator design pattern in Java:

Let’s first take a look at the class diagram of the decorator design pattern:

A preliminary study on Java decorator design patterns

As you can see from the picture, we can decorate any implementation class of the Component interface, and these implementation classes also include the decorator itself, and the decorator itself can also be decorated again.

The following is a simple decorator design pattern implemented in Java. It provides a decorator system that starts with the basic addition of coffee and can continue to add milk, chocolate, and sugar.

interface Component {
  void method();
}
class Coffee implements Component {
 
  @Override
  public void method() {
    // TODO Auto-generated method stub
    System.out.println("倒入咖啡");
  }
   
}
class Decorator implements Component {
  public Component comp;
  public Decorator(Component comp) {
    this.comp = comp;
  }
  @Override
  public void method() {
    // TODO Auto-generated method stub
    comp.method();
  }
   
}
class ConcreteDecorateA extends Decorator {
  public Component comp;
  public ConcreteDecorateA(Component comp) {
    super(comp);
    this.comp = comp;
  }
  public void method1() {
    System.out.println("倒入牛奶");
  }
  public void method2() {
    System.out.println("加入糖 ");
  }
  public void method() {
    super.method();
    method1();
    method2();
  }
}
class ConcreteDecorateB extends Decorator {
  public Component comp;
  public ConcreteDecorateB(Component comp) {
    super(comp);
    this.comp = comp;
  }
  public void method1() {
    System.out.println("加入巧克力");
  }
  public void method() {
    super.method();
    method1();
  }
}
public class TestDecoratePattern {
  public static void main(String[] args) {
    Component comp = new Coffee();
    comp.method();
    System.out.println("--------------------------------------------------");
    Component comp1 = new ConcreteDecorateA(comp);
    comp1.method();
    System.out.println("--------------------------------------------------");
    Component comp2 = new ConcreteDecorateB(comp1);
    comp2.method();
    System.out.println("--------------------------------------------------");
    Component comp3 = new ConcreteDecorateB(new ConcreteDecorateA(new Coffee()));
    comp3.method();
    System.out.println("--------------------------------------------------");
    Component comp4 = new ConcreteDecorateA(new ConcreteDecorateB(new Coffee()));
    comp4.method();
  }
}

Run results:

A preliminary study on Java decorator design patterns

The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope that everyone will support the PHP Chinese website.

For more articles related to the preliminary exploration of Java decorator design patterns, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn