Java implementation decorator pattern

Decorator pattern is a structural design pattern that allows for the dynamic addition of new behavior to an object without the need for inheritance. This pattern can wrap and decorate existing objects without affecting them, thereby expanding their functionality. Applicable scenarios: When it is necessary to dynamically add some additional functionality to an object without modifying its original code. When multiple functions need to be added to an object transparently and dynamically in sequence. 3. When special operations need to be performed on an object, but the source code of the object does not want to be modified. The benefits of this design pattern include: 1. The decorator mode makes it easy to extend the functionality of an object, and allows for flexible addition or removal of decorators to dynamically adjust functionality as needed. 2. Using the decorator pattern can avoid using too many subclasses to extend the functionality of objects, thereby reducing the number of classes. The following is a complete sample code of Java that demonstrates how to dynamically extend the functionality of objects using the decorator pattern: //Define an interface to represent a component public interface Component { void operation(); } //Specific component implementation classes public class ConcreteComponent implements Component { @Override public void operation() { System. out. println ("Perform basic operations"); } } //Abstract Decorator Class public abstract class Decorator implements Component { protected Component component; public Decorator(Component component) { this.component = component; } @Override public void operation() { component.operation(); } } //Specific Decorative Class A public class ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component component) { super(component); } @Override public void operation() { super.operation(); System. out. println ("Extended Function A"); } } //Specific Decorative Class B public class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } @Override public void operation() { super.operation(); System. out. println ("Extended Function B"); } } //Using the Decorator pattern to extend the functionality of objects public class Main { public static void main(String[] args) { Component component = new ConcreteComponent(); component = new ConcreteDecoratorA(component); component = new ConcreteDecoratorB(component); component.operation(); } } In this example, the Component interface represents a component, and ConcreteComponent is the specific component implementation class. Decorator is an abstract decorator class that holds a Component object and implements the Component interface. ConcreteDecoratorA and ConcreteDecoratorB are specific decorator classes that inherit decorators and add specific functions. In the Main class, we first create a ConcreteComponent object, and then decorate it one by one through ConcreteDecoratorA and ConcreteDecoratorB, thereby extending the functionality of the object. Finally, when the component. operation() method is called, the original and new functions will be executed in the order defined in the Decorator class.