23 design patterns (6) Java Decorator Pattern

23 design patterns Part 6: Java Decorator Pattern

definition:

Dynamically extend the function of an object without changing the inheritance used by the original class file and the original class. It is realized by creating a wrapper object, that is, wrapping the real object with decoration.

Role:

Abstract component role (project): give an interface to specify the object ready to receive additional responsibilities. Specific component role (employee): define a class that will receive additional responsibilities. Decoration role (Manager): hold an instance of component object and define an interface consistent with the abstract component interface. Specific decoration role (ManagerA, managerb): responsible for "pasting" additional responsibilities to component objects.

Example:

Common interface:

Decorated object:

Decoration object:

Test:

It can be seen from the example that the original oldperson class is not changed, and its subclass is not defined, but the extension of person is realized, which is the function of decorator mode.

advantage:

1. Using decorator mode is more flexible than using inheritance, because it chooses to extend the function of an object in a dynamic way. Different decorators can be selected at run time to achieve different behaviors.

2. By using different specific decorative classes and the arrangement and combination of these decorative classes, many combinations of different behaviors can be created. You can use multiple concrete decoration classes to decorate the same object to get more powerful objects.

3. Concrete component class and concrete decoration class can be changed independently, and they can be low coupling. Users can add new specific component classes and specific decoration classes as needed, and then make various combinations when using them. The original code does not need to be changed and conforms to the "opening and closing principle".

Disadvantages:

1. Many small objects will be generated, which increases the complexity of the system

2. This feature, which is more flexible than inheritance, also means that the decoration mode is more prone to errors than inheritance, and troubleshooting is also very difficult. For objects decorated for many times, it may be cumbersome to find errors level by level during debugging.

Difference between decorator and adapter mode:

1. Adapter mode is mainly used to be compatible with those classes that can not work together, so that they can be transformed into compatible target interfaces. Although it can also add new responsibilities like decorators, the purpose is not here.

The decorator mode is mainly to add new responsibilities to the decorated.

2. The adapter mode is to use the new interface to call the original interface, which is invisible or unavailable to the new system. The decorator mode uses the original interface intact, and the system also uses the decorated objects through the original interface.

3. The adapter knows the details of the adapter (that is, the class or interface).

The decorator only knows what its interface is, and its specific type (whether it is a base class or other derived classes) is only known during operation.

The difference between decorator and inheritor:

Inheritance:

Advantages: the code structure is clear and the implementation is simple. Disadvantages: for each class that needs to be enhanced, specific subclasses should be created to help it enhance, which will lead to too large inheritance system.

Decorator:

Advantages: multiple classes that need to be enhanced can be enhanced internally through polymorphism technology. Disadvantages: instances of classes that need to be enhanced need to be maintained internally through polymorphism technology. This makes the code a little more complex.

Usage scenario:

1. You need to extend the functionality of a class or add additional responsibilities to a class. 2. You need to add functions to an object dynamically. These functions may be ambiguous or temporary, and can be easily revoked dynamically at any time. 3. It is necessary to add a large number of functions generated by the arrangement and combination of some basic functions, so as to make the inheritance relationship unrealistic. 4. When the method of generating subclasses cannot be used for expansion. In one case, there may be a large number of independent extensions. In order to support each combination, a large number of subclasses will be generated, resulting in an explosive increase in the number of subclasses. Another possibility is that the class definition is hidden or cannot be used to generate subclasses.

Transferred from: Java confidant

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>