Home > Backend Development > PHP Tutorial > Detailed explanation of the decorator pattern of PHP design patterns

Detailed explanation of the decorator pattern of PHP design patterns

韦小宝
Release: 2023-03-17 14:22:02
Original
2031 people have browsed it

Decorator model can dynamically add and modify the function of a class. Each class provides a function. If you want to modify and add additional functions, traditional programming It is to write a subclass to inherit it and reimplement the class method, using the decorator pattern, and only need to add a decorator## at runtime. #Object can be implemented to achieve maximum flexibility

Detailed explanation of the decorator pattern of PHP design patterns

<?php

/*
 * 装饰模式
 */

abstract class Beverage
{
    public $_name;

    abstract public function Cost();
}

// 被装饰者类
class Coffee extends Beverage
{
    public function construct()
    {
        $this->_name = &#39;Coffee&#39;;
    }

    public function Cost()
    {
        return 1.00;
    }
}

// 以下三个类是装饰者相关类
class CondimentDecorator extends Beverage //装饰类
{
    public function construct()
    {
        $this->_name = &#39;Condiment&#39;;
    }

    public function Cost()
    {
        return 0.1;
    }
}

class Milk extends CondimentDecorator //牛奶 配料 --装饰者
{
    public $_beverage;

    public function construct($beverage)
    {
        if ($beverage instanceof Beverage) {
            $this->_beverage = $beverage;
        } else
            exit(&#39;Failure&#39;);
    }

    public function Cost()
    {
        return $this->_beverage->Cost() + 0.2;
    }
}

class Sugar extends CondimentDecorator //糖 配料 --装饰者
{
    public $_beverage;

    public function construct($beverage)
    {
        $this->_name = &#39;Sugar&#39;;
        if ($beverage instanceof Beverage) {
            $this->_beverage = $beverage;
        } else {
            exit(&#39;Failure&#39;);
        }
    }

    public function Cost()
    {
        return $this->_beverage->Cost() + 0.2;
    }
}

// Test Case
//1.拿杯咖啡
$coffee = new Coffee();

//2.加点牛奶
$coffee = new Milk($coffee);

//3.加点糖
$coffee = new Sugar($coffee);

echo $coffee->Cost();
echo $coffee->_name;
Copy after login

The decoration mode reduces the coupling of the system, and can dynamically add or delete the responsibilities of objects, and This allows the specific component classes and specific decoration classes that need to be decorated to be changed independently, so that new specific component classes and specific decoration classes can be added

Related recommendations:

Detailed sample code for PHP decoration mode

php decoration mode

Easy to understand PHP Design pattern singleton pattern

The above is the detailed content of Detailed explanation of the decorator pattern of PHP design patterns. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template