“This is the 26th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

What is decorator mode

Decorator pattern: Allows you to add functionality to an existing object without changing its structure. Common applications include servlets, Java IO streams, etc. For example, with an IO stream, a FileInputStream can read a file and then wrap it with an ObjectInputStream. When reading with a stream, you use the ObjectInputStream functionality. For example, the original IO stream could only read files, but after using other streams it has new capabilities. Decorator mode extends its functionality without changing the original IO stream.

Second, the realization of decorator pattern

I will use the cup and decorative cup as an example to achieve. The cup itself is an abstract concept, and then there is a concrete glass. And then the abstract decorator, I decorated this cup with colors, decorated with cartoon stickers.

public interface AbstractGlass {
    void show();
}
Copy the code

Abstract component, cup

Public class implements AbstractGlass{@override public void show() {system.out.println (" this is a glass "); }}Copy the code

Specific components, specific cups.

public abstract class GlassDecorator implements AbstractGlass{ protected AbstractGlass decoratorGlass; public GlassDecorator(AbstractGlass decoratorGlass){ this.decoratorGlass = decoratorGlass; } @Override public void show() { decoratorGlass.show(); }}Copy the code

Abstract decorator role, which has an abstract cup as a property (this property is the cup that needs to be decorated) and implements the abstract cup interface. This interface is implemented by calling the methods of the cup itself that you want to decorate, and can then be enhanced from there.

public class PinkGlassDecorator extends GlassDecorator{ public PinkGlassDecorator(AbstractGlass decoratorGlass) { super(decoratorGlass); } @Override public void show() { decoratorGlass.show(); System.out.println(" The cup is pink "); }}Copy the code

Specific decoration role, here the cup is decorated with pink.

public class StickerGlassDecorator extends GlassDecorator{ public StickerGlassDecorator(AbstractGlass decoratorGlass) { super(decoratorGlass); } @Override public void show() { decoratorGlass.show(); System.out.println(" Cup with cartoon stickers "); }}Copy the code

Decorate the characters and put cartoon stickers on the cups.

public static void main(String[] args) {
    ConcreateGlass concreateGlass = new ConcreateGlass();
    concreateGlass.show();
    System.out.println("----");
    PinkGlassDecorator pinkGlassDecorator = new PinkGlassDecorator(concreateGlass);
    pinkGlassDecorator.show();
    System.out.println("----");
    StickerGlassDecorator stickerGlassDecorator = new StickerGlassDecorator(concreateGlass);
    stickerGlassDecorator.show();
    System.out.println("----");
    StickerGlassDecorator stickerAndPinkGlass = new StickerGlassDecorator(pinkGlassDecorator);
    stickerAndPinkGlass.show();
}
Copy the code

The original cup was obtained by using the cup alone. You can then decorate the character by adding colors and stickers.

Third, summary

Roles in Decorator mode:

  1. Component: Abstract. Component is our original object. The example above refers to the abstract cup.
  2. ConcreateComponent: Concrete concrete or base concrete, concrete cups, basic cups, which can be added with decorations or used alone.
  3. Decorator: Decorator role, typically an abstract class that inherits an abstract component that defines a component property. For example, the above example defines an abstract cup.
  4. ConcreateDecorator: Concrete decorator role.