Decorator mode of design mode

What is decorator mode

Official explanation: Decorator Pattern allows you to add new functions to an existing object without changing its structure. This type of design pattern belongs to structural pattern, which is a wrapper for existing classes.

Usage scenario

For example, a pancake fruit must first have a pancake, and then add scallion, coriander, crispy cake, roast sausage, fried dough sticks and other accessories on the basis of the pancake.

advantage

It can dynamically and flexibly add and delete class functions, solve the problem of excessive inheritance, and comply with the single principle and opening and closing principle of the design principle.

Sketch Map

Traditional mode

  Decorator mode

It can be seen that the traditional model uses the method of unlimited inheritance. If there are too many additional products, it is bound to cause class explosion. Moreover, if you want to modify the intermediate additional products, you also need to modify the class that inherits the additional products. For example, if customers want to remove coriander, the three categories of crispy cake, fried dough sticks and sausage will be affected, which is very inflexible and the chef will be very annoyed.

The decorator mode is used to solve these problems. You can see that all add ons inherit only one common abstract class, and the pancake class is encapsulated separately. At this time, except for the modification of the abstract class of the add-on, each other class has its own responsibilities and does not affect each other, so you can flexibly add add an add-on to the pancake.

Of course, the text explanation is just to impress people. If you want to understand it, you have to look at the code, and use it more and practice it more in the project

Code example

  directory structure

  Jianbing.php pancake class

<?php

/**
 * Pancakes
 * Class Jianbing
 */
class Jianbing{
    public $name;
    public $fujiapin=[];

    //Initialize pancake name
    public function __construct($name)
    {
        $this->name = $name;
    }

    //Add additional items
    public function addFujiapin(Fujiapin $fujiapin){
        $this->fujiapin[] = $fujiapin;
    }

    //Get the final pancake
    public function get(){
        echo $this->name;
        foreach ($this->fujiapin as $fujiapin){
            echo "<br>";
            $fujiapin->get();

        }
    }
}

Fujiapin.php add-on abstract class

/**
 * Add on abstract class
 * Class Fujiapin
 */
abstract class Fujiapin{
    //Get add ons
    abstract function get();
}

Conghua.php scallion class

class Conghua extends Fujiapin{
    public function get()
    {
        // TODO: Implement get() method.
        echo "Add scallion";
    }
}

Xiangcai.php coriander class

class Xiangcai extends Fujiapin{
    public function get()
    {
        // TODO: Implement get() method.
        echo "Add coriander";
    }
}
Youtiao.php fried dough sticks class
class Youtiao extends Fujiapin{
    public function get()
    {
        // TODO: Implement get() method.
        echo "Refueling bar";
    }
}

kaochang.php roast sausage class

<?php
class Kaochang extends Fujiapin{
    public function get()
    {
        // TODO: Implement get() method.
        echo "Add roast sausage";
    }
}

Cuibing.php cracker class

class Cuibing extends Fujiapin{
    public function get()
    {
        // TODO: Implement get() method.
        echo "Add crispy cake";
    }
}

Client.php call

<?php
require_once 'Fujiapin.php';
require_once 'Conghua.php';
require_once 'Kaochang.php';
require_once 'Jianbing.php';
require_once 'Youtiao.php';
require_once 'Cuibing.php';
require_once 'Xiangcai.php';

$jianbing = new Jianbing('grilled savory crepe');

$conghua = new Conghua();
$xiangcai = new Xiangcai();
$youtiao = new Youtiao();

$jianbing->addFujiapin($conghua);//Add scallion
$jianbing->addFujiapin($xiangcai);//Add coriander
$jianbing->addFujiapin($youtiao);//Add fried dough sticks

$jianbing->get();

Operation results

 

Tags: Design Pattern

Posted on Fri, 03 Dec 2021 00:21:05 -0500 by ub_kh