工厂模式告一段落,我们来研究其他一些模式。不知道各位大佬有没有尝试过女装?据说女装大佬程序员很多哟。其实,今天的装饰器模式就和化妆这件事很像。相信如果有程序媛MM在的话,马上就能和你讲清楚这个设计模式。
装饰这两个字,我们暂且把他变成化妆。首先你得有一张脸,然后打底,然后上妆,可以早上来个淡妆上班,也可以下班的时候补成浓妆出去嗨。当然,码农们下班的时间点正好是能赶上夜场的下半场的。话说回来,不管怎么化妆,你的脸还是你的脸,有可能可以化成别人不认识的另一个人,但这的的确确还是你的脸。这就是装饰器,对对象(脸)进行各种装饰(化妆),让这个脸更好看(增加职责)。
GoF定义:动态地给一个对象添加一些额外的职责,就增加功能来说,Decorator模式相比生成子类更为灵活
GoF类图
代码实现
interface Component{ public function operation();}class ConcreteComponent implements Component{ public function operation(){ echo "I'm face!" . PHP_EOL; }}
interface Component{
public function operation();
}
class ConcreteComponent implements Component{
public function operation(){
echo "I'm face!" . PHP_EOL;
很简单的一个接口和一个实现,这里我们就把具体的实现类看作是一张脸吧!
abstract class Decorator implements Component{ protected $component; public function __construct(Component $component){ $this->component = $component; }}
abstract class Decorator implements Component{
protected $component;
public function __construct(Component $component){
$this->component = $component;
抽象的装饰者类,实现Component接口,但并不实现operation()方法,让子类去实现。在这里主要保存一个Componet的引用,一会就要对他进行装饰。对应到上方的具体类,我们就是要准备给脸化妆啦!
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; }}
class ConcreteDecoratorA extends Decorator{
public $addedState = 1; // 没什么实际意义的属性,只是区别于ConcreteDecoratorB
echo $this->component->operation() . "Push " . $this->addedState . " cream!" . PHP_EOL;
class ConcreteDecoratorB extends Decorator{
$this->component->operation();
$this->addedBehavior();
// 没什么实际意义的方法,只是区别于ConcreteDecoratorA
public function addedBehavior(){
echo "Push 2 cream!" . PHP_EOL;
两个具体装饰者。在这里我是涂了两次霜,毕竟是纯爷们,对化妆这事儿真的是不了解。好像第一步应该先是打粉底吧?不过这次就这样,我们这两个装饰器实现的就是给脸上涂两层霜。
手机这玩意干不过某米、某O、某为,这没法玩呀,好吧,哥们去专心做手机壳吧!嗯,我先准备了一个透明壳(Component),貌似有点丑,没办法,谁叫哥们穷。给某米的加上各种纯色(DecoratorA1),然后背后印上各种颜色的植物(DecoratorB1)吧;某O的手机最近喜欢找流量明显做代言,那我给他的手机壳就用各种炫彩色(DecoratorA2)和明星的卡通头像(DecoratorB2);最后的某为,好像手机已经开始引领业界潮流了,折叠屏这玩意不是要砸我这卖手机壳的生意嘛!!好吧,哥不给你们做了,还是跟我的某米、某O混去吧!!
完整代码:装饰器模式
继续来发短信,之前我们用工厂模式解决了多个短信运营商的问题。这回我们要解决的是短信内容模板的问题。对于推广类的短信来说,根据最新的广告法,我们是不能出现“全国第一”、“全世界第一”这类的词语的,当然,一些不太文明的用语我们也是不能使用的。
现在的情况是这样的,我们有一个很早之前的短信模板类,里面的内容是固定的,老系统依然还是使用这个模板,老系统是面对的内部员工,对语言内容的要求不高。而新系统则需要向全网发送,也就是内外部的用户都要发送。这时,我们可以用装饰器模式来对老系统的短信模板进行包装。其实说简单点,我们就是用装饰器来做文本替换的功能。好处呢?当然是可以不去改动原来的模板类中的方法就实现了对老模板内容的修改扩展等。
短信发送类图
完整源码:短信发送装饰器方法
<?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
// 短信模板接口
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
return str_replace('全国第一', '全国第二', $this->template->message());
// 使用我们的大数据部门同事自动生成的新词库来过滤敏感词汇,这块过滤不是强制要过滤的内容,可选择使用
class SensitiveFilterDecoratorMessage extends DecoratorMessageTemplate
public $bigDataFilterWords = ['牛X'];
public $bigDataReplaceWords = ['好用'];
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);
// 过滤完了,发送吧
说明
关注公众号:【硬核项目经理】获取最新文章
添加微信/QQ好友:【xiaoyuezigonggong/149844827】免费得PHP、项目管理学习资料
知乎、公众号、抖音、头条搜索【硬核项目经理】
B站ID:482780532
原文链接:http://www.cnblogs.com/zyblog-coder/p/14131495.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728