Simple discussion on decoration mode

Simple discussion on decoration mode

1. Brief description of decoration mode

Decoration pattern is a design pattern used to replace inheritance. It dynamically adds responsibilities to objects without defining subclasses, and uses the association relationship between objects to replace the inheritance relationship between classes. It reduces the coupling of the system and can dynamically add or delete the responsibilities of objects.

Dynamically add some additional responsibilities to an object. In terms of expanding functionality, decoration patterns provide a more flexible alternative than using subclasses.

Decorator mode is also called packaging mode( Wrapper Pattern) is a structural design pattern.

This mode dynamically extends objects in a transparent way to the client (open to extension and closed to modification)

At the same time, this pattern is also one of the alternative methods of inheritance relationship.

In short, it dynamically adds some additional responsibilities to the object. Similar to iron man, it can assemble different weapons.

Decoration mode, which can be understood as follows. Is to add behavior to a class. For example, human beings are the parent. Men and women are subclasses. If you use inheritance, there will be two more classes, but with decoration mode, I will directly decorate human beings into men or women without inheritance. It directly reduces the coupling between classes

2. Definition of decoration mode

The definition of Decorator Pattern: refers to the pattern that dynamically adds some responsibilities (i.e. adds its additional functions) to the object without changing the existing object structure. It belongs to object structure pattern.

3. UML diagram of decoration pattern

  • Component (Abstract construction): it is the parent class of concrete construction class and abstract decoration class. Declare the business method of the concrete construction class, which can make the client treat the decorated class and the unmodified class equally. Realize the transparent operation of the client.

  • Concretecomponent (concrete component class): it implements the business methods of the abstract construction class, and the decoration class will add additional methods to it.

  • Decorator (decoration class): it is also a subclass of the abstract construction class, which is used to add methods to the concrete construction class. It maintains a reference to the abstract construction object. Through this reference, the method of constructing the object before decoration can be called, and the method can be extended through its subclass, which has achieved the purpose of decoration.

  • Concretedecorator (concrete decoration class): it is a subclass of the abstract decoration class and is responsible for adding new methods to the construction class. Each specific decoration class defines a specific behavior.

4. Application examples of decoration mode

For example, we have received a demand to process Bitmap, which has various functions: beautiful pupil, thin face, filter, skin grinding, old film and other effects.

package work_02;

public abstract class Component {
    public abstract void display();
}
package work_02;

public class ConcreteComponent extends Component {

    @Override
    public void display() {
        // TODO Auto-generated method stub
        System.out.println("Display Form ");
    }
}
package work_02;

public class ConcreteDecorator extends Decorator {

    public ConcreteDecorator(Component com) {
        super(com);
        // TODO Auto-generated constructor stub
    }
    //override method 
    public void display() {
        super.display();
        this.setMyMethod();
    }
    public void setMyMethod() {
        System.out.println("Add decoration method");
    }
}
package work_02;

public class ConcreteDecorator2 extends Decorator {

    public ConcreteDecorator2(Component com) {
        super(com);
        // TODO Auto-generated constructor stub
    }

    //Override the display method
    public void display() {
        super.display();
        this.shou();
    }

    public void shou() {
        System.out.println("This is decoration method 2");
    }
}
package work_02;

public class Decorator extends Component {
    private Component component;
    public Decorator(Component com) {
        this.component=com;
    }

    @Override
    public void display() {
        // TODO Auto-generated method stub
        component.display();
    }

}

The test classes are as follows

package work_02;

public class Client {
    public static void main(String[] args) {
        System.out.println("-------------------------------");
        Component com = new ConcreteComponent();
        Component d1 = new ConcreteDecorator(com);
        d1.display();
        System.out.println("-------------------------------");
        //The method after decoration can also be decorated again
        Component d2 = new ConcreteDecorator2(d1);
        d2.display();
        System.out.println("-------------------------------");
    }
}

5. Requirements for transparency

The transparency of decoration mode to the client requires the program not to declare a variable of type ConcreteComponent, but to declare a variable of type Component.

Take the example of the monkey king. We must always treat all the changes of the monkey king as the monkey king. If we treat the fish that the monkey king becomes as the fish instead of the monkey king, we will be cheated by the monkey king, and this should not happen. The following is right.

TheGreatestSage sage = new Monkey();
TheGreatestSage bird = new Bird(sage);

The following is wrong:

Monkey sage = new Monkey();
Bird bird = new Bird(sage);

6. Translucent decoration mode

However, pure decorative patterns are difficult to find. The purpose of decoration pattern is to enhance the performance of the considered classes without changing the interface. When enhancing performance, it is often necessary to establish new public methods. Even in sun Dasheng's system, new methods are needed. For example, Qi Tian Da Sheng does not have the ability to fly, but birds do. This means that birds should have a new fly() method. For another example, Qi Tian Da Sheng doesn't have the ability to swim, while the fish has, which means that the fish should have a new swim() method.

This leads to the realization of most decoration patterns are "translucent" rather than completely transparent. In other words, the decoration mode is allowed to change the interface and add new methods. This means that the client can declare a variable of type ConcreteDecorator, so that it can call the methods only available in the ConcreteDecorator class:

TheGreatestSage sage = new Monkey();
Bird bird = new Bird(sage);
bird.fly();

The translucent decoration mode is between the decoration mode and the adapter mode. The purpose of the adapter pattern is to change the interface of the considered class. You can also enhance or change the function of the considered class by rewriting one or more methods or adding new methods. Most decorative patterns are actually translucent decorative patterns, which are also called semi decorative and semi adapter patterns.

6. Summary

Decoration mode definition

Decoration mode: dynamically add some additional responsibilities to an object. In terms of expanding functionality, decoration patterns provide a more flexible alternative than using subclasses.

constitute

  • Component (Abstract construction): it is the parent class of concrete construction class and abstract decoration class. Declare the business method of the concrete construction class, which can make the client treat the decorated class and the unmodified class equally. Realize the transparent operation of the client.

  • Concretecomponent (concrete component class): it implements the business methods of the abstract construction class, and the decoration class will add additional methods to it.

  • Decorator (decoration class): it is also a subclass of the abstract construction class, which is used to add methods to the concrete construction class. It maintains a reference to the abstract construction object. Through this reference, the method of constructing the object before decoration can be called, and the method can be extended through its subclass, which has achieved the purpose of decoration.

  • Concretedecorator (concrete decoration class): it is a subclass of the abstract decoration class and is responsible for adding new methods to the construction class. Each specific decoration class defines a specific behavior.

Precautions for use

Decoration pattern is very easy to use when inheritance is inconvenient. After all, there is no need to write too many classes. But I suggest not to decorate too many times, because obviously, if the decoration is too multi-layer, it will be very troublesome for troubleshooting.

advantage

  • For expanding the function of an object, decoration mode is more flexible than inheritance mode and will not lead to a sharp increase in the number of classes.

  • The function of a class can be expanded in a dynamic way. The same configuration file can select different decoration classes in Jining at run time.

  • An object can be decorated many times. Different combinations of behaviors can be created by using different specific decoration classes and the arrangement and combination of these decoration classes.

Disadvantages:

  • When using decoration mode for system design, many small objects will be generated. The difference between these objects is that they are connected in different ways, rather than their class or attribute values. A large number of small objects are bound to produce a large part of system resource overhead. Affect system performance.

  • Decoration pattern is a more flexible solution than inheritance. But at the same time, it also means that it is more error prone and more difficult to queue than inheritance. For objects with multi-layer decoration, they need to be checked level by level, which is cumbersome.

Usage scenario

  • Add responsibilities to a single object in a dynamic and transparent manner without affecting other objects.

  • When inheritance cannot be used for expansion.

7. Relevant books

  • Dahua design mode

8. Related links

Keywords: Java Design Pattern

Added by seanpaulcail on Fri, 11 Feb 2022 13:32:53 +0200