Design Patterns---Decorator Pattern

1 Definition

The Decorator pattern, also known as the Wrapper pattern, is an alternative to inheritance. It refers to dynamically adding some functions to an object without changing the structure of the object.

2 Advantages and disadvantages

2.1 Advantages

  • Decorator is a powerful complement to inheritance, which is more flexible than inheritance. Without changing the original object, it can dynamically extend functions to an object, plug and play.
  • Different effects can be achieved by using non-decorative classes and the permutation and combination of these decorative classes
  • The decorator pattern fully adheres to the open-closed principle

2.2 Disadvantages

  • The decorator pattern will add many subclasses, and overuse will increase the complexity of the program.

3 Structure and implementation of the decorator pattern

The class diagram for the Decorator pattern looks like this:

 The decorator pattern mainly includes the following roles.

  1. Abstract Component role: Define an abstract interface to standardize objects that are ready to receive additional responsibilities.
  2. ConcreteComponent roles: Implement abstract components and add some responsibilities to them by decorating roles.
  3. Abstract Decorator role: inherits abstract components and contains instances of concrete components, which can extend the functions of concrete components through their subclasses.
  4. ConcreteDecorator role: Implements methods related to abstract decoration and adds additional responsibilities to concrete component objects.

4 Code implementation

Everyone should have seen a Japanese cartoon "Tiejia Xiaobao", the No. 1 robot in it - Kabuda, in normal times, he is a clumsy but very cute robot, when they need to fight for peace When the star is in the game, he will transform into a Kabuda with outstanding combat power, and finally, he gets his Kabuda Giant, which raises the combat power to a new level. In this example, transformed Kabuda and Kabuda Giant are decorations of ordinary Kabuda, enhancing Kabuda's abilities.

4.1 KaBuDa interface KaBuDa

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-07 12:01
 **/
public interface KaBuDa {

    /**
     * 示人的形象
     */
    void display();
}

4.2 CommonKaBuDa

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-07 14:49
 **/
public class CommonKaBuDa implements KaBuDa{

    @Override
    public void display() {
        System.out.println("我是普通卡布达,卡布卡布卡布");
    }
}

4.3 TransfigurationKaBuDa

**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-07 15:20
 **/
public class TransfigurationKaBuDa extends Decorator{

    public TransfigurationKaBuDa(KaBuDa kaBuDa) {
        super(kaBuDa);
    }

    @Override
    public void display() {
        super.display();
        System.out.println("启动超级变换形态---超级变换形态");
    }
}

4.4 GiantKaBuDa

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-07 15:49
 **/
public class GiantKaBuDa extends Decorator{

    public GiantKaBuDa(KaBuDa kaBuDa) {
        super(kaBuDa);
    }

    @Override
    public void display() {
        super.display();
        System.out.println("超级卡布达巨人");
    }
}

4.5 Main program

/**
 * @program: design-pattern-learning
 *
 * @author: zgr
 * @create: 2021-09-07 11:57
 **/
public class MainClass {
    public static void main(String[] args) {
        System.out.println("-----------------1--------------------");
        KaBuDa commonKaBuDa = new CommonKaBuDa();
        commonKaBuDa.display();
        System.out.println("-----------------2--------------------");
        System.out.println("蜻蜓队长: 比赛开始");
        System.out.println("-----------------3--------------------");
        KaBuDa transfigurationKaBuDa = new TransfigurationKaBuDa(commonKaBuDa);
        transfigurationKaBuDa.display();
        System.out.println("-----------------4--------------------");
        System.out.println("呼唤卡布达巨人");
        System.out.println("-----------------5--------------------");
        KaBuDa giantKaBuDa = new GiantKaBuDa(transfigurationKaBuDa);
        giantKaBuDa.display();
    }
}

4.6 Running Results

 As a result, it was seen that Kabuda was constantly decorated, and finally reached the invincible state of having Kabuda giants.

5 Simplification of the decorator pattern

Under normal circumstances, the decorator pattern does not have an abstract decorated interface, but only a specific decorated object. This is to consider removing Componentthe class (interface) and treating it Decoratoras a ConcreteComponentsubclass. As shown below:

6 Quotes

1. "Dahua Design Patterns"

2. Detailed explanation of decorator pattern (decoration design pattern)

3. java design pattern - decorator pattern (Decorator)

7 Source code

https://github.com/airhonor/design-pattern-learning/tree/main/src/com/hz/design/pattern/decorator

Guess you like

Origin blog.csdn.net/honor_zhang/article/details/120161258