Decorator Mode of Design Mode
Decorator mode: Dynamically decorate an object with additional functionality.
1. What is the decorator mode?
Decorator mode is to dynamically decorate an object with additional functionality. To extend functionality, the decorator provides a more resilient alternative than inheritance and is more flexible than generating subclasses.
Usually in inheritance relationships, new subclasses are needed to extend functionality, while Decorator mode can dynamically extend an object's functionality without extending subclasses.
The Decorator Mode class diagram is as follows:
Component: Gives an abstract interface to specify objects that receive additional functionality.
ConcreteComponent: Defines a class that will receive additional functionality.
Decorat: Holds an instance of a component object and defines an interface consistent with the component interface.
Specific Decorator (ConcretDecorat1,ConcretDecorat2): Responsible for adding additional functionality to the component object.
Test class (DecoratTest): Test whether the decoration was successful.
2. Specific code
1.Component class
The code is as follows (example):
/** * Abstract component */ public abstract class Component { public abstract void operate(); }
2.ConcreteComponent class
The code is as follows (example):
/** * Specific components */ public class ConcreteComponent extends Component { @Override public void operate() { System.out.println("Specific components"); } }
3.Decorat classes
The code is as follows (example):
/** * Decorator */ public abstract class Decorat extends Component { private Component component; public Decorat(Component component){ this.component = component; } @Override public void operate() { component.operate(); } }
4.ConcretDecorat class
The code is as follows (example):
/** * Decorator 1 */ public class ConcreteDecorat1 extends Decorat { public ConcreteDecorat1(Component component) { super(component); } private void method1(){ System.out.println("Decoration Method 1"); } @Override public void operate() { method1(); super.operate(); } }
/** * Decorator 2 */ public class ConcreteDecorat2 extends Decorat { public ConcreteDecorat2(Component component) { super(component); } private void method1(){ System.out.println("Decoration Method 2"); } @Override public void operate() { method1(); super.operate(); } }
5.DecoratTest class
The code is as follows (example):
/** * @author haowu * @create 2021-09-25-21:41 * @Description Test Class */ public class DecoratTest { @Test public void testDecorat() { Component concreteComponent = new ConcreteComponent(); concreteComponent = new ConcreteDecorat1(concreteComponent); concreteComponent = new ConcreteDecorat2(concreteComponent); concreteComponent.operate(); } }
Run Results
summary
From the test class, we can see that the client can add functions freely, the client can dynamically execute the order of placing, each person can place in a different order, and the decorator mode can make different assemblies to complete the change of requirements.
Decoration mode allows the system to dynamically decide to add a required "decoration" or remove an unneeded "decoration". Inheritance is different. Inheritance is static and it is determined before the system runs. Many combinations of different behaviors can be created by using different specific decorative classes and the arrangement and combination of these decorative classes.
Note: Text explanations are referenced in https://zhuanlan.zhihu.com/p/65113766