Dynamicly add some additional responsibilities to an object. In terms of adding functions, the Decorator mode is more flexible than generating subclasses [GOF95]
Decoration mode dynamically attaches more responsibilities to an object in a way that is transparent to the client. This means that the client will not feel any difference between the object before decoration and after decoration. Decoration pattern can extend the functionality of an object without creating more subclasses.
1. Decoration mode structure diagram
2. Main characters in decoration mode
Abstract Component role: Define an object interface to standardize objects that are ready to receive additional responsibilities, so that responsibilities can be dynamically added to these objects.
Concrete Component role: Define a class that will receive additional responsibilities.
Decorator role: holds a pointer to the Component object and defines an interface consistent with the Component interface.
Concrete Decorator role: Responsible for adding additional responsibilities to component objects.
3. Advantages and Disadvantages of Decoration Mode
Advantages of Decoration Mode:
1. More flexible than static inheritance;
2. Avoid having too many features in classes at higher levels of the hierarchy
Disadvantages of Decoration Mode:
1. Using decoration mode will generate more objects than using inheritance relationship. And these objects all look imaginary, making troubleshooting difficult.
4. Applicable Scenarios of Decoration Mode
1. Add responsibilities to a single object in a dynamic and transparent manner without affecting other objects.
2. Handle those responsibilities that can be revoked, that is, you need to dynamically add functions to an object and these functions can be dynamically revoked.
3. When the method of generating a subclass cannot be expanded. In one case, there may be a large number of independent extensions, and a large number of subclasses will be generated to support each combination, causing the number of subclasses to explode. Another case could be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
5. Decoration mode PHP example
<?php /** * 抽象构件角色 */ interface Component { /** * 示例方法 */ public function operation(); } /** * 装饰角色 */ abstract class Decorator implements Component{ protected $_component; public function __construct(Component $component) { $this->_component = $component; } public function operation() { $this->_component->operation(); } } /** * 具体装饰类A */ class ConcreteDecoratorA extends Decorator { public function __construct(Component $component) { parent::__construct($component); } public function operation() { parent::operation(); // 调用装饰类的操作 $this->addedOperationA(); // 新增加的操作 } /** * 新增加的操作A,即装饰上的功能 */ public function addedOperationA() { echo 'Add Operation A <br />'; } } /** * 具体装饰类B */ class ConcreteDecoratorB extends Decorator { public function __construct(Component $component) { parent::__construct($component); } public function operation() { parent::operation(); $this->addedOperationB(); } /** * 新增加的操作B,即装饰上的功能 */ public function addedOperationB() { echo 'Add Operation B <br />'; } } /** * 具体构件 */ class ConcreteComponent implements Component{ public function operation() { echo 'Concrete Component operation <br />'; } } /** * 客户端 */ class Client { /** * Main program. */ public static function main() { $component = new ConcreteComponent(); $decoratorA = new ConcreteDecoratorA($component); $decoratorB = new ConcreteDecoratorB($decoratorA); $decoratorA->operation(); $decoratorB->operation(); } } Client::main(); ?>
The above is the code for using PHP to implement decoration mode. There are also some conceptual distinctions about decoration mode. I hope it will be helpful to everyone's learning.