java Decorator Pattern

Posted by gateway69 on Sat, 01 Jan 2022 22:53:24 +0100

1, Decorator mode definition
1. Dynamically add some additional responsibilities to an object. In terms of adding functions, decoration pattern is more flexible than generating subclasses

Component abstract component
Component is an interface or abstract class, which is to define our core objects, that is, the most primitive objects. Defining an object interface can dynamically add responsibilities to these objects
ConcreteComponent concrete component
ConcreteComponent is the implementation of the most core, primitive and basic interface or abstract class. What needs to be decorated is its Decorator decorative role 

Generally, it is an abstract class. In its properties, there must be a private variable pointing to the Component abstract Component. 

Concrete decorator a and concrete decorator B are two specific decoration classes, which should decorate the most core, primitive and basic things into other things

/**
 * Abstract component
 */
public abstract class Componet {
    /**
     * Abstract method
     */
    public abstract void operate();
}

/**
 * Specific components
 */
public class ConcreteComponet extends Componet {
    @Override
    public void operate() {
        //Specific implementation
        System.out.println("do something");
    }
}

/**
 * Decoration class
 */
public class Decorator extends Componet {
    private Componet componet = null;

    /**
     * Pass the modifier through the constructor
     *
     * @param componet
     */
    public Decorator(Componet componet) {
        this.componet = componet;
    }

    /**
     * Entrusted to the person to be repaired
     */
    @Override
    public void operate() {
        this.componet.operate();
    }
}
/**
 * Specific decoration
 */
public class ConcreteDecoratorA extends Decorator {
    /**
     * Pass the modifier through the constructor
     *
     * @param componet
     */
    public ConcreteDecoratorA(Componet componet) {
        super(componet);
    }

    /**
     * Define your own modification methods
     */
    private void method1() {
        System.out.println("modification DecoratorA");
    }

    @Override
    public void operate() {
        this.method1();
        super.operate();
    }
}
/**
 * Specific decoration
 */
public class ConcreteDecoratorB extends Decorator {

    /**
     * Pass the modifier through the constructor
     *
     * @param componet
     */
    public ConcreteDecoratorB(Componet componet) {
        super(componet);
    }

    /**
     * Define your own modification methods
     */
    private void method2() {
        System.out.println("Modification method DecoratorB");
    }

    @Override
    public void operate() {
        //Override the operate method of the parent class
        this.method2();
        super.operate();
    }
}
public class Client {
    public static void main(String[] args) {
        Componet componet = new ConcreteComponet();
        //First modification
        componet = new ConcreteDecoratorA(componet);
        //Second modification
        componet= new ConcreteDecoratorB(componet);
        //Run after modification
        componet.operate();
    }
}

2, Advantages of decoration mode
1. Decorated classes and decorated classes can develop independently without coupling each other. In other words, the Component class does not need to know the Decorator class. The Decorator class extends the functions of the Component class from the outside, and the Decorator does not need to know the specific components
2. Decoration pattern is an alternative to inheritance. Let's look at the decoration class Decorator. No matter how many layers of decoration, the returned object is still a Component, and the implementation is still an is-a relationship
3. Decoration pattern can dynamically extend the function of an implementation class
3, Disadvantages of decoration mode
The multi-layer decoration is more complex, just like peeling onions. You peel them until you finally find that there is a problem with the innermost decoration
4, Examples

● abstract component role: played by InputStream. This is an abstract class that provides a unified interface for each subtype.

● concrete component role: played by ByteArrayInputStream, FileInputStream, PipedInputStream, StringBufferInputStream, etc. They implement the interfaces specified by the abstract component roles.

● decorator role: played by FilterInputStream. It implements the interface specified by InputStream.

● concrete decorator role: it is played by several classes, namely BufferedInputStream, DataInputStream and two uncommon classes LineNumberInputStream and PushbackInputStream.
4, Usage scenario
1. You need to extend the function of a class or add additional functions to a class. 

2. You need to dynamically add functions to an object, and these functions can be dynamically revoked.
3. It is necessary to modify or add functions for a batch of brothers, and the decoration mode is preferred. 

5, Difference from agent mode
Proxy mode focuses on the access to the proxy object;
Decorator mode focuses on adding additional functions to the decorated object.

Topics: Design Pattern Algorithm