1. Concept:
Adorner mode, also known as decorator mode, is the ability to dynamically extend an object without having to change the original class file and use inheritance. The traditional programming pattern is that the subclass inherits the overloads of the parent class implementation method, using the adorner mode, just add a new adorner object, more flexible, avoid the number of classes and too many levels.
2. Role:
Component (The Decorated object base class): Defines an object interface to standardize the objects that are prepared to accept additional responsibilities.
Concretecomponent (specifically decorated object): a specific component role, which is the class that will be decorated to add functionality.
Decorator (Decorator base Class): Adorner interface.
Concretedecorator (Specific decorator Class): A concrete adorner that adds responsibilities to the component.
3. Example:
Requirements Description
A food and beverage system consisting of a staple food and a matching dish, with a staple and a matching dish, each with different categories and different prices. To ensure the scalability of the system (modify the price, increase or decrease the need for staple food/dish type), use the adorner mode.
Organization structure:
Code Show:
\libs\component.php
<? phpnamespace Libs; /* */interface component{ /* * * * calculate the required cost * * public function cost ();}
\libs\concretecomponentbun.php
<?phpnamespace Libs;/** define the specific object to be decorated, here is the staple bun **/classConcretecomponentbunImplementscomponent{ Public function__construct () {Echo"Oh,there is the construct of concretecomponentbun.</br>"; } Public functionCost () {EchoThere is the function of Concretecomponentbun named cost. <br/> "; return15.00; }}
\libs\decorator.php
<? Phpnamespace Libs; /* */classimplements component{publicfunction __construct () { $this->_name = ' Decorator '; } Public function Cost () { return 1.00; }}
\libs\concretedecoratorsalad.php
<?phpnamespace Libs;classConcretedecoratorsaladextendsdecorator{ Public $_component; Public function__construct ($component) { Echo"Oh,there is the construct of concretedecoratorsalad.</br>"; if($componentinstanceof Component) { $this->_component =$component; } Else { Exit(' Failure '); } } Public functionCost () {EchoThere is the function of Concretedecoratorsalad named cost. <br/> "; return $this->_component->cost (+10.00); }}
\libs\concretedecoratorsoup.php
<?phpnamespace Libs;classConcretedecoratorsoupextendsdecorator{ Public $_component; Public function__construct ($component) { Echo"Oh,there is the construct of concretedecoratorsoup.</br>"; if($componentinstanceof Component) { $this->_component =$component; } Else { Exit(' Failure '); } } Public functionCost () {EchoThere is the function of Concretedecoratorsoup named cost. <br/> "; return $this->_component->cost (+5.00); }}
\libs\usedecorator.php
<?phpnamespace Libs;classusedecorator{ Public Static functionindex () {$bun=NewConcretecomponentbun (); $cost=$bun-Cost (); Echo"The cost is {$cost} <br> "; Echo"_________________________________________________</br>"; $salad _bun=NewConcretedecoratorsalad ($bun); $cost=$salad _bun-Cost (); Echo"The cost is {$cost} <br> "; Echo"_________________________________________________</br>"; $soup _bun=NewConcretedecoratorsoup ($bun); $cost=$soup _bun-Cost (); Echo"The cost is {$cost} <br> "; Echo"_________________________________________________</br>"; }}
Call
Libs\usedecorator::index ();
Results:
Oh,there is the construct of Concretecomponentbun.there are the function of the Concretecomponentbun named cost. The cost was _________________________________________________oh,there is the construct of Concretedecoratorsalad.there is the function of Concretedecoratorsalad named cost. There is the function of Concretecomponentbun named cost. The cost was _________________________________________________oh,there is the construct of Concretedecoratorsoup.there is the function of Concretedecoratorsoup named cost. There is the function of Concretecomponentbun named cost. The cost is 20 _________________________________________________
4. Pros and cons
1). The purpose of the decorator mode and 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.
1). This is more flexible than inheritance, but it also means more complexity.
2). Decoration 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.
Recommended Reading >>
The decorator mode of PHP design mode
PHP design mode-Adorner mode
The decorator mode of PHP design mode
The decorator mode of PHP design mode
PHP design mode-Adorner mode
PHP design mode-Adorner mode
PHP design mode Series-Adorner mode (Decorator)
——— decorator mode for PHP design mode
Adorner mode application Scenario
PHP design mode-Adorner mode