Java Design Patterns--Decorator Pattern

1 Decorator Pattern decorator pattern

Purpose: To add new behavior to an object without changing its own function, that is, to enhance the function;
Implementation: Introduce the modified class into the abstract modified class through aggregation, and hand over the details of the enhanced function to the subclass for implementation.

1. In order to increase the function without adding many subclasses;
2. Dynamically add some additional responsibilities to an object. In terms of adding object functions, the decoration mode is more flexible than generating subclasses;
3. With Compared with the proxy mode, the decorator mode emphasizes adding new functions.

2 Implementation

Code scene: In Jedi Lifesaving, everyone likes the 98k very much. If you pick up a 6x mirror or an 8x mirror, it will be even more H. Find a place to wait for the opportunity happily and put a gun behind your back;
the 98k still works without any accessories. It can be used, but after the 8x mirror is installed, it can be used for long-distance aiming and enhance its accuracy. The 8x mirror is a decoration relative to the 98k;

2.1 Code Implementation

Abstract Component Role: Weapon Interface

public interface Weapon {
    // 入参为 倍镜的精确度
    void use(int precision);
}

Specific component role: Mauser 98k

public class Mauser98k implements Weapon {

    public Mauser98k() {
        System.out.println("毛瑟 98k步枪射击精度高,
        经加装4倍、6倍光学瞄准镜后,可作为一种优秀的狙击步枪使用~");
    }
    @Override
    public void use(int precision) {
        if (precision >= 6) {
            System.out.println("让子弹飞一会,敌人倒下后,传来98k低沉的回声~");
        }
        if (precision < 4) {
            System.out.println("距离有点远 打偏了~");
        }
    }
}

Abstract decorative role: high magnification abstract class

public abstract class HighPowerLens implements Weapon {
    // 将98k聚合进来
    protected Mauser98k m98k;

    public HighPowerLens(Mauser98k m98k) {
        this.m98k = m98k;
    }
}

Specific decoration category: eight times mirror

public class EightTimesScope extends HighPowerLens {
    // 八倍镜的精度是8
    private final int precision = 8;

    // 调用父类构造器
    public EightTimesScope(Mauser98k m98k) {
        super(m98k);
    }

    @Override
    public void use(int precision) {
        // 调用瞄准功能
        this.aim();
        // 加装8倍镜后 增强了98k精确度
        // 使用98k进行射击
        m98k.use(this.precision);
    }

    // 瞄准方法
    private void aim() {
        System.out.println("把十字准星对准了对方的沟子~");
    }
}

2.2 Involving roles

The following roles are included in the decoration pattern structure diagram:

Component (abstract component): It is the common parent class of concrete components and abstract decoration classes. It declares business methods implemented in concrete components. Its introduction enables clients to process undecorated objects in a consistent manner and after decoration. An object that implements transparent operations on the client side.

ConcreteComponent (concrete component): It is a subclass of the abstract component class, used to define a concrete component object, implements the methods declared in the abstract component, and the decorator can add additional responsibilities (methods) to it.

Decorator (abstract decoration class): It is also a subclass of abstract component class, which is used to add responsibilities to specific components, but the specific responsibilities are implemented in its subclasses. It maintains a reference to the abstract component object, through which the method of the decorated component object can be called, and the method is extended through its subclasses to achieve the purpose of decoration.

ConcreteDecorator: It is a subclass of the abstract decorator class and is responsible for adding new responsibilities to the component. Each concrete decorator class defines some new behaviors, it can call the methods defined in the abstract decorator class, and can add new methods to extend the behavior of the object.

2.3 call

caller:

public class Client {
    public static void main(String[] args) {
        // 使用没有装8倍镜的98k打一枪
        Mauser98k m98k = new Mauser98k();
        System.out.println("剧情:距离太远不好瞄准--------");
        // 距离有点远可能会打偏
        // 没有安装倍镜 所以倍镜精度传入0
        m98k.use(0);

        System.out.println("剧情:舔包捡到一个8倍镜--------");
        // 使用装了8倍镜的98k打一枪
        EightTimesScope ets = new EightTimesScope(m98k);
        // 通过8倍镜的精确瞄准
        ets.use(8);
    }
}

result:

毛瑟 98k步枪射击精度高,经加装4倍、6倍光学瞄准镜后,可作为一种优秀的狙击步枪使用~
剧情:距离太远不好瞄准--------
距离有点远 打偏了~
剧情:舔包捡到一个8倍镜--------
把十字准星对准了对方的沟子~
让子弹飞一会,敌人倒下后,传来98k低沉的回声~

Code address: click to jump

References:
[1] Graphical Design Patterns/(Japanese) Hiroshi Yuki; translated by Yang Wenxuan. – Beijing: People’s Posts and Telecommunications Press, 2017.1.
[ 2 ] Wikipedia Design
Patterns [ 3 ] Geek Academy WIKI – Design Patterns .
[ 4 ] Rookie Tutorial – Design Patterns .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325462968&siteId=291194637