Decorator Pattern Concepts
The ability to dynamically extend an object without having to change the original class file and using inheritance is to wrap the real object by creating a wrapper object, which is the adornment.
Decorator Mode Features
1. Decorative objects and real objects have the same interface. This allows the client object to interact with the adornment object in the same way as the real object.
2. Adornment object contains a reference to a real object (reference)
3. The Adornment object accepts all requests from the client. It forwards these requests to the real object.
4. Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance.
Applicability
1. You need to extend the functionality of a class or add additional responsibilities to a class.
2. The need to dynamically add functionality to an object, these functions can be re-dynamic revocation.
3. It is necessary to increase the number of functions resulting from the permutations of some basic functions, thus making the inheritance relationship impractical.
4. When a method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
Advantages
1. The purpose of the decorator pattern and the inheritance relationship is to extend the functionality of the object, but decorator can provide more flexibility than inheritance.
2. Designers can create a combination of many different behaviors by using different decorative classes and arranging combinations of these decorations.
Disadvantages
1. This is more flexible than inheritance, but it also means more complexity.
2. Decorating mode causes many small classes to appear in the design, which can complicate the program if overused.
3. The adornment mode is programmed for the abstract component (Component) type. However, if you are programming for a specific component, you should rethink your application architecture and whether the decorator is appropriate. Of course, you can change the component interface, add new public behavior, and realize the "translucent" decorator pattern. Make the best choice in the actual project.
Design principles
1. Multi-use combination, less inheritance.
2. Classes should be designed to be open to extensions and closed for modification.
Adorner mode instance
Decorators[] = $decorator; } function Addextrafunction () { foreach ($this->decorators as $decorator) { $decorator->saymsg () } } function Test () { $this->addextrafunction (); echo "I am Test"; }} $mailTest = new MailTest (), $decorator 1 = new Decorator1 (); $decorator 2 = new Decorator2 (); $mailTest->adddecorator ($ Decorator1); $mailTest->adddecorator ($decorator 2); $mailTest->test ();