The decorator pattern allows adding new functionality to an existing object without changing its structure. This article takes you through the decorator pattern in PHP, introduces the benefits of decorators and the scenarios they are most suitable for.
The factory pattern comes to an end, let’s study some other patterns. I wonder if any of you guys have ever tried women’s clothing? It is said that there are many female programmers. In fact, today's decorator pattern is very similar to makeup. I believe that if Program Yuan MM is here, I can explain this design pattern to you immediately.
Decoration, let’s turn it into makeup for now. First you have to have a face, then apply foundation, and then put on makeup. You can put on light makeup to go to work in the morning, or you can put on heavy makeup when you get off work to go out and have fun. Of course, the time when the programmers get off work happens to be in time for the second half of the night show. Having said that, no matter how you put on makeup, your face is still your face. You may be able to transform into another person that others don't recognize, but it is still your face. This is the decorator, which performs various decorations (makeup) on the object (face) to make the face look better (increase responsibilities).
GoF definition: dynamically add some additional responsibilities to an object. In terms of adding functionality, the Decorator pattern is more flexible than generating subclasses
GoF class diagram:
Code implementation:
interface Component{ public function operation(); } class ConcreteComponent implements Component{ public function operation(){ echo "I'm face!" . PHP_EOL; } }
A very simple interface and an implementation, here we will put the specific implementation class Think of it as a face!
abstract class Decorator implements Component{ protected $component; public function __construct(Component $component){ $this->component = $component; } }
The abstract decorator class implements the Component interface, but does not implement the operation() method, allowing subclasses to implement it. Here we mainly save a reference to Component, which will be decorated later. Corresponding to the specific categories above, we are going to prepare to put makeup on the face!
class ConcreteDecoratorA extends Decorator{ public $addedState = 1; // 没什么实际意义的属性,只是区别于ConcreteDecoratorB public function operation(){ echo $this->component->operation() . "Push " . $this->addedState . " cream!" . PHP_EOL; } } class ConcreteDecoratorB extends Decorator{ public function operation(){ $this->component->operation(); $this->addedBehavior(); } // 没什么实际意义的方法,只是区别于ConcreteDecoratorA public function addedBehavior(){ echo "Push 2 cream!" . PHP_EOL; } }
Two concrete decorators. I applied cream twice here. After all, I am a pure man and I really don’t know much about makeup. It seems like the first step should be to apply foundation, right? But this time, what our two decorators achieve is to apply two layers of frost on the face.
This mobile phone thing can’t be used by a certain Mi, a certain O, and a certain Wei. It’s impossible to play. Well, buddy, go and concentrate on making mobile phone cases! Well, I first prepared a transparent shell (Component), which looks a bit ugly, but there is nothing I can do about it. Who calls me poor? Add various solid colors (DecoratorA1) to a certain meter, and then print plants of various colors (DecoratorB1) on the back; a certain O's mobile phone recently likes to be endorsed by traffic, so I will use various colors for his mobile phone case. Dazzling colors (DecoratorA2) and celebrity cartoon avatars (DecoratorB2); the last thing is that it seems that mobile phones have begun to lead the industry trend. Isn’t this folding screen thing going to ruin my business of selling mobile phone cases? ! Okay, I won’t do it for you, so just hang out with my so-and-so Mi and so-and-so O! !
Full code: Decorator pattern
https://github.com/zhangyue0503/designpatterns-php/blob/master/04.decorator/source/decorator. php
Continue to send text messages. Previously, we used factory mode to solve the problem of multiple SMS operators. This time we have to solve the problem of text message content templates. For promotional text messages, according to the latest advertising law, we cannot use words such as "No. 1 in the country" or "No. 1 in the world". Of course, we cannot use some uncivilized terms.
现在的情况是这样的,我们有一个很早之前的短信模板类,里面的内容是固定的,老系统依然还是使用这个模板,老系统是面对的内部员工,对语言内容的要求不高。而新系统则需要向全网发送,也就是内外部的用户都要发送。这时,我们可以用装饰器模式来对老系统的短信模板进行包装。其实说简单点,我们就是用装饰器来做文本替换的功能。好处呢?当然是可以不去改动原来的模板类中的方法就实现了对老模板内容的修改扩展等。
短信发送类图:
完整源码:短信发送装饰器方法
https://github.com/zhangyue0503/designpatterns-php/blob/master/04.decorator/source/message-decorator.php
<?php // 短信模板接口 interface MessageTemplate { public function message(); } // 假设有很多模板实现了上面的短信模板接口 // 下面这个是其中一个优惠券发送的模板实现 class CouponMessageTemplate implements MessageTemplate { public function message() { return '优惠券信息:我们是全国第一的牛X产品哦,送您十张优惠券!'; } } // 我们来准备好装饰上面那个过时的短信模板 abstract class DecoratorMessageTemplate implements MessageTemplate { public $template; public function __construct($template) { $this->template = $template; } } // 过滤新广告法中不允许出现的词汇 class AdFilterDecoratorMessage extends DecoratorMessageTemplate { public function message() { return str_replace('全国第一', '全国第二', $this->template->message()); } } // 使用我们的大数据部门同事自动生成的新词库来过滤敏感词汇,这块过滤不是强制要过滤的内容,可选择使用 class SensitiveFilterDecoratorMessage extends DecoratorMessageTemplate { public $bigDataFilterWords = ['牛X']; public $bigDataReplaceWords = ['好用']; public function message() { return str_replace($this->bigDataFilterWords, $this->bigDataReplaceWords, $this->template->message()); } } // 客户端,发送接口,需要使用模板来进行短信发送 class Message { public $msgType = 'old'; public function send(MessageTemplate $mt) { // 发送出去咯 if ($this->msgType == 'old') { echo '面向内网用户发送' . $mt->message() . PHP_EOL; } else if ($this->msgType == 'new') { echo '面向全网用户发送' . $mt->message() . PHP_EOL; } } } $template = new CouponMessageTemplate(); $message = new Message(); // 老系统,用不着过滤,只有内部用户才看得到 $message->send($template); // 新系统,面向全网发布的,需要过滤一下内容哦 $message->msgType = 'new'; $template = new AdFilterDecoratorMessage($template); $template = new SensitiveFilterDecoratorMessage($template); // 过滤完了,发送吧 $message->send($template);
说明
推荐学习:《PHP视频教程》
The above is the detailed content of A brief discussion on the decorator pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!