PHP设计模式之装饰器模式

        装饰模式是以对客户透明的方式动态地给一个对象附加上更多的职责。这也就是说,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。

PHP设计模式之装饰器模式_第1张图片
书中自有颜如玉,书中自有黄金屋

装饰模式中主要角色

        抽象构件(Component)角色:定义一个对象接口,以规范准备接收附加职责的对象,从而可以给这些对象动态地添加职责。

        具体构件(Concrete Component)角色:定义一个将要接收附加职责的类。

        装饰(Decorator)角色:持有一个指向Component对象的指针,并定义一个与Component接口一致的接口。

        具体装饰(Concrete Decorator)角色:负责给构件对象增加附加的职责。

PHP设计模式之装饰器模式_第2张图片
书中自有颜如玉,书中自有黄金屋

装饰模式的优缺点

装饰模式的优点:

1、比静态继承更灵活;

2、避免在层次结构高层的类有太多的特征

装饰模式的缺点:

使用装饰模式会产生比使用继承关系更多的对象。并且这些对象看上去都很想像,从而使得查错变得困难。

PHP设计模式之装饰器模式_第3张图片
书中自有颜如玉,书中自有黄金屋

装饰模式适用场景

1、在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。

2、处理那些可以撤消的职责,即需要动态的给一个对象添加功能并且这些功能是可以动态的撤消的。

3、当不能彩生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

PHP设计模式之装饰器模式_第4张图片
书中自有颜如玉,书中自有黄金屋

装饰模式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 '; 

        }

}

/** * 具体装饰类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 '; 

        }

}

/** * 具体构件 */

class ConcreteComponent implements Component

        public function operation()

        {  

                echo 'Concrete Component operation '; 

        }

}

/** * 客户端 */

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();

PHP设计模式之装饰器模式_第5张图片
书中自有颜如玉,书中自有黄金屋  

装饰器模式-在不改变原有类的结构上,对类的功能那个作补充

//武器基类

abstract class Weapon

{  

        abstract public function descriptions();  

        abstract public function cost();

}

//剑类

class Glave extends Weapon

{  

        public function descriptions()

        {    

                return 'Glave';  

        }  

        public function cost()

        {    

                return "100";  

        }

}

//匕首类

class Knife extends Weapon

{  

        public function descriptions()

        {    

                return __CLASS__;  

        }  

        public function cost()

        {    

                return "80";  

        }

}

//斧类

class Axe extends Weapon

{  

        public function descriptions()

        {    

                return __CLASS__;  

        }  

        public function cost()

        {    

                return "200";  

        }

}

//属性类

class Property extends Weapon

{  

        protected $_weapon = null;  

        protected $_price = 0;  

        protected $_descriptions = '';  

        public function __construct(Weapon $weapon)

        {    

                $this->_weapon = $weapon;  

        }  

        public function cost()

        {    

                return   $this->_weapon->cost() + $this->_price;  

        }  

        public function descriptions()

        {    

                return $this->_weapon->descriptions().$this->_descriptions;  

        }

}

//力量属性

class Strength extends Property

{  

        protected $_price = 30;  

        protected $_descriptions = '+ Strength';

}

//敏捷属性

class Agility extends Property

{  

        protected $_price = 50;  

        protected $_descriptions = '+ Agility';

}

//智力属性

class Intellect extends Property

{  

        protected $_price = 20;  

        protected $_descriptions = '+ Intellect';

}

$weapon = new Agility(new Strength(new Strength(new Glave())));

echo $weapon->cost();

echo $weapon->descriptions();

PHP设计模式之装饰器模式_第6张图片
书中自有颜如玉,书中自有黄金屋

        图片与文章无关,在阅读技术文档的时候欣赏一下美女也是一个不错的选择,希望可以对你有帮助,如果有错误的地方欢迎指正。

你可能感兴趣的:(PHP设计模式之装饰器模式)