What is the Decorator mode?
Dynamically add some extra responsibilities to an object. In terms of adding functions, the Decorator mode is more flexible than generating subclasses.
1. Structure
Component: Defines an object interface that can dynamically add responsibilities to these objects.
interface Component { public void operation();}ConcreteComponent: Implement the interface defined by Component. class ConcreteComponent implements Component { @Override public void operation() { System.out.println("Initial Behavior"); }} Decorator: Decorate abstract class, inherits Component, extends the functions of Component class from external classes, but for Component, there is no need to know the existence of Decorator.
class Decorator implements Component { // Hold a Component object and form an aggregation relationship with Component protected Component component; // Pass in the object to be further modified public Decorator(Component component) { this.component = component; } @Override // Call the original method to be modified public void operation() { component.operation(); }} ConcreteDecorator: Specific decorative objects play the function of adding responsibilities to Component.
class ConcreteDecoratorA extends Decorator { private String addedState = "New Property 1"; public ConcreteDecoratorA(Component component) { super(component); } public void operation() { super.operation(); System.out.println("Add attribute: " + addedState); }}class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } public void operation() { super.operation(); AddedBehavior(); } public void AddedBehavior() { System.out.println("Add Behavior"); }} Test code
public class DecoratorPattern { public static void main(String[] args) { Component component = new ConcreteComponent(); component.operation(); System.out.println("====================================================================================================================================================================================================================================================================================================================================================================================================================================================================== System.out.println("================================================================================================ System.out.output(); }} Running results
Initial behavior===========================================================================================================================================================================================================================================================
2. Application scenarios
1. It is necessary to add responsibilities to one object dynamically and transparently, that is, it does not affect other objects.
2. You need to dynamically add functions to an object, and these functions can be dynamically undoed.
3. It is necessary to add a large number of functions generated by the arrangement and combination of some basic functions, so that the inheritance relationship becomes unrealistic.
4. When the method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions, which will produce a large number of subclasses to support each combination, causing the number of subclasses to explode. Another case may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
3. Key points
1. Decorative objects and real objects have the same interface. This way the client object can interact with the decorative object in the same way as the real object.
2. The decorative object contains a reference to a real object.
3. The decorative object accepts all requests from the client. It forwards these requests to the real object.
4. Decorative objects can add some additional functions before or after forwarding these requests. This ensures that at runtime, additional functions can be added externally without modifying the structure of a given object. In object-oriented design, functional extensions to a given class are usually achieved through inheritance.
The above is an introduction to the relevant content about the Java Decorator Mode, I hope it will be helpful to everyone's learning.