Home  >  Article  >  Backend Development  >  A brief discussion on the decorator pattern in PHP

A brief discussion on the decorator pattern in PHP

青灯夜游
青灯夜游forward
2021-06-23 18:40:392890browse

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.

A brief discussion on the decorator pattern in PHP

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.

Gof class diagram and explanation

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:

A brief discussion on the decorator pattern in PHP

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.

  • It can be seen from the code that we have been packaging the specific ConcreteComponent object
  • Going further, we are actually packaging its operation() method Twice, each time adding a little something based on the previous one
  • Don’t get hung up on the added attributes and methods on the A and B decorators, they are just used to distinguish between these in the GoF class diagram. The two decorators are not the same thing. Each decorator can do many other things. The Component object does not necessarily have only the operation() method. We can selectively decorate all or part of the methods in the object
  • It seems that we have all inherited Component. Can't we just subclass it and rewrite it all the way? Why go to all this trouble? Dear, please understand the concept of combination. Our Decorator parent class contains a reference to a real object, which decouples itself. We only wrap the real object. Please do not instantiate the decorator directly to use it directly.
  • Still don’t understand? What are the benefits? Do you dare to randomly change the classes and methods of the old system? When you want to add new functions to the awesome code written by your ex, you might as well try decorators, it might work wonders!

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

Example

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.

现在的情况是这样的,我们有一个很早之前的短信模板类,里面的内容是固定的,老系统依然还是使用这个模板,老系统是面对的内部员工,对语言内容的要求不高。而新系统则需要向全网发送,也就是内外部的用户都要发送。这时,我们可以用装饰器模式来对老系统的短信模板进行包装。其实说简单点,我们就是用装饰器来做文本替换的功能。好处呢?当然是可以不去改动原来的模板类中的方法就实现了对老模板内容的修改扩展等。

短信发送类图:

A brief discussion on the decorator pattern in PHP

完整源码:短信发送装饰器方法

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 &#39;优惠券信息:我们是全国第一的牛X产品哦,送您十张优惠券!&#39;;
    }
}

// 我们来准备好装饰上面那个过时的短信模板
abstract class DecoratorMessageTemplate implements MessageTemplate
{
    public $template;
    public function __construct($template)
    {
        $this->template = $template;
    }
}

// 过滤新广告法中不允许出现的词汇
class AdFilterDecoratorMessage extends DecoratorMessageTemplate
{
    public function message()
    {
        return str_replace(&#39;全国第一&#39;, &#39;全国第二&#39;, $this->template->message());
    }
}

// 使用我们的大数据部门同事自动生成的新词库来过滤敏感词汇,这块过滤不是强制要过滤的内容,可选择使用
class SensitiveFilterDecoratorMessage extends DecoratorMessageTemplate
{
    public $bigDataFilterWords = [&#39;牛X&#39;];
    public $bigDataReplaceWords = [&#39;好用&#39;];
    public function message()
    {
        return str_replace($this->bigDataFilterWords, $this->bigDataReplaceWords, $this->template->message());
    }
}

// 客户端,发送接口,需要使用模板来进行短信发送
class Message
{
    public $msgType = &#39;old&#39;;
    public function send(MessageTemplate $mt)
    {
        // 发送出去咯
        if ($this->msgType == &#39;old&#39;) {
            echo &#39;面向内网用户发送&#39; . $mt->message() . PHP_EOL;
        } else if ($this->msgType == &#39;new&#39;) {
            echo &#39;面向全网用户发送&#39; . $mt->message() . PHP_EOL;
        }

    }
}

$template = new CouponMessageTemplate();
$message = new Message();

// 老系统,用不着过滤,只有内部用户才看得到
$message->send($template);

// 新系统,面向全网发布的,需要过滤一下内容哦
$message->msgType = &#39;new&#39;;
$template = new AdFilterDecoratorMessage($template);
$template = new SensitiveFilterDecoratorMessage($template);

// 过滤完了,发送吧
$message->send($template);

说明

  • 装饰器的最大好处:一是不改变原有代码的情况下对原有代码中的内容进行扩展,开放封闭原则;二是每个装饰器完成自己的功能,单一职责;三是用组合实现了继承的感觉;
  • 最适用于:给老系统进行扩展
  • 要小心:过多的装饰者会把你搞晕的
  • 不一定都是对同一个方法进行装饰,其实装饰者应该更多的用于对对象的装饰,对对象进行扩展,这里我们都是针对一个方法的输出进行装饰,但仅限此文,装饰器的应用其实更加广泛
  • 装饰器的特点是全部都继承自一个主接口或类,这样的好处就是返回的对象是相同的抽象数据,具有相同的行为属性,否则,就不是装饰之前的对象,而是一个新对象了
  • 有点不好理解没关系,我们这次的例子其实也很勉强,这个设计模式在《Head First设计模式》中有提到Java的I/O系列接口是使用的这种设计模式:FileInputStream、LineNumberInputStream、BufferInputStream等
  • Laravel框架中的中间件管道,这里其实是多种模式的综合应用,其中也应用到了装饰器模式:Laravel HTTP——Pipeline 中间件装饰者模式源码分析
  • 另外在Laravel中,日志处理这里也是对Monolog进行了装饰,有兴趣的同学可以去了解下

推荐学习:《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!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete