java design patterns series of decorator patterns

  • 2020-05-05 11:12:20
  • OfStack

What is the decorator mode (Decorator)?
Dynamically add some additional responsibilities to an object. In terms of adding functionality, Decorator mode is more flexible than subclassing.
1. Structure

Component: defines an object interface to which responsibilities can be dynamically added.


interface Component {
  public void operation();
}
ConcreteComponent :  implementation  Component  The defined interface. 
class ConcreteComponent implements Component {
  @Override
  public void operation() {
    System.out.println(" The initial behavior ");
  }
}

Decorator: decorates the abstract class, inherits Component, and extends the functionality of the Component class from the outer class, but for Component, there is no need to know that Decorator exists.


class Decorator implements Component {
  //  To hold a  Component  Object, and  Component  Form an aggregation relationship 
  protected Component component;
  
  //  Pass in the object to be further decorated 
  public Decorator(Component component) {
    this.component = component;
  }
  
  @Override
  //  Invokes the original method of the object to be decorated 
  public void operation() {
    component.operation();
  }
}

ConcreteDecorator: specific decoration objects that add responsibilities to Component.


class ConcreteDecoratorA extends Decorator {
  private String addedState = " New properties 1";
  
  public ConcreteDecoratorA(Component component) {
    super(component);
  }
  
  public void operation() {
    super.operation();
    System.out.println(" Add attributes : " + addedState);
  }
}

class ConcreteDecoratorB extends Decorator {
  public ConcreteDecoratorB(Component component) {
    super(component);
  }

  public void operation() {
    super.operation();
    AddedBehavior();
  }
  
  public void AddedBehavior() {
    System.out.println(" Add the behavior ");
  }
}

Test code


public class DecoratorPattern {
  public static void main(String[] args) {
    Component component = new ConcreteComponent();
    component.operation();
    
    System.out.println("======================================");
    Decorator decoratorA = new ConcreteDecoratorA(component);
    decoratorA.operation();
    
    System.out.println("======================================");
    Decorator decoratorB = new ConcreteDecoratorB(decoratorA);
    decoratorB.operation();
  }
}

Run the result


 The initial behavior 
======================================
 The initial behavior 
 Add attributes :  New properties 1
======================================
 The initial behavior 
 Add attributes :  New properties 1
 Add the behavior 

Ii. Application scenarios

1. Add responsibility to one object dynamically and transparently, that is, without affecting other objects.
2. Add functions to an object dynamically, and these functions can be revoked dynamically.
3. The need to add a very large number of functions resulting from the arrangement and combination of some basic functions makes the inheritance relationship unrealistic.
4. When the method of subclassing cannot be used for expansion. In one case, there may be a large number of independent extensions, and to support each combination will result in a large number of subclasses, with the number of subclasses exploding. Another situation might be because the class definition is hidden, or because the class definition cannot be used to subclass.

3. Key points

1. Decorated objects and real objects have the same interface. This allows the client object to interact with the decorated object in the same way as the real object.

2. A decoration object contains a reference to a real object (reference).

3. The decoration object accepts all requests from the client. It forwards these requests to real objects.

4. The decorator object can add additional functionality before or after forwarding these requests. This ensures that at run time, additional functionality can be added externally without modifying the structure of a given object. In object-oriented design, it is usually through inheritance to achieve the functionality of a given class extension.

The above is about the java decorator model related content introduction, hope to help you to learn.


Related articles: