当前位置: 移动技术网 > IT编程>软件设计>设计模式 > PHP设计模式—装饰器模式

PHP设计模式—装饰器模式

2020年05月04日  | 移动技术网IT编程  | 我要评论

今天多少号了,家装效果图大全,五环童话伴奏

 

定义:

装饰器模式(decorator):动态的给一个对象添加一些额外的职责,就增加功能来说,装饰器比生成子类更加灵活。

 

结构:

  • component:定义一个对象接口,可以给这些对象动态地添加职责。
  • concretecomponent:定义了一个具体的对象,也可以给这个对象添加一些职责。
  • decorator:装饰抽象类,继承了 component ,从外类来扩展 component 类的功能,但对于 component 来说,是无需知道 decorator 的存在的。
  • concretedecorator:具体的装饰对象,起到给 component 添加职责的功能。

 

代码实例:

这里以一个游戏角色为例,角色本身自带基础攻击属性,也可以通过额外的武器装备增加属性值。这里的装备武器就是动态的给角色添加额外的职责。
1、角色role.php,对应component

/**
 * 角色,抽象类
 * class role
 */
abstract class role
{
    /**
     * @return mixed
     */
    abstract public function getname();

    /**
     * @return mixed
     */
    abstract public function getaggressivity();
}

2、武器arms.php,对应concretecomponent

/**
 * 武器,继承抽象类
 * class arms
 */
class arms extends role
{
    /**
     * 基础攻击力
     * @var int
     */
    private $aggressivity = 100;

    /**
     * @return string
     */
    public function getname()
    {
        // todo: implement getname() method.
        return '基础攻击值';
    }

    /**
     * @return int
     */
    public function getaggressivity()
    {
        // todo: implement getaggressivity() method.
        return $this->aggressivity;
    }

}

3、装饰抽象类roledecorator.php,对应decorator

/**
 * 装饰抽象类
 * class roledecorator
 */
abstract class roledecorator extends role
{
    /**
     * @var role
     */
    protected $role;

    /**
     * roledecorator constructor.
     * @param role $role
     */
    public function __construct(role $role)
    {
        $this->role = $role;
    }
}

4、剑sword.php,对应concretedecorator

/**
 * 剑,具体装饰对象,继承装饰抽象类
 * class sword
 */
class sword extends roledecorator
{
    /**
     * @return mixed|string
     */
    public function getname()
    {
        // todo: implement getname() method.
        return $this->role->getname() . '+斩妖剑';
    }

    /**
     * @return int|mixed
     */
    public function getaggressivity()
    {
        // todo: implement getaggressivity() method.
        return $this->role->getaggressivity() + 200;
    }
}

5、枪gun.php,对应concretedecorator

/**
 * 枪,具体装饰对象,继承装饰抽象类
 * class gun
 */
class gun extends roledecorator
{
    /**
     * @return mixed|string
     */
    public function getname()
    {
        // todo: implement getname() method.
        return $this->role->getname() . '+震天戟';
    }

    /**
     * @return int|mixed
     */
    public function getaggressivity()
    {
        // todo: implement getaggressivity() method.
        return $this->role->getaggressivity() + 150;
    }
}

6、调用

// 基础攻击值
$arms = new arms();
echo $arms->getname();
echo $arms->getaggressivity() . '<br>';

// 基础攻击值+斩妖剑
$sword = new sword(new arms());
echo $sword->getname();
echo $sword->getaggressivity() . '<br>';

// 基础攻击值+震天戟
$gun = new gun(new arms());
echo $gun->getname();
echo $gun->getaggressivity() . '<br>';

// 基础攻击值+斩妖剑+震天戟
$person = new gun(new sword(new arms()));
echo $person->getname();
echo $person->getaggressivity() . '<br>';

7、结果:

基础攻击值100
基础攻击值+斩妖剑300
基础攻击值+震天戟250
基础攻击值+斩妖剑+震天戟450

 

总结:

  • 装饰器模式就是为已有功能动态地添加更多功能地一种方式。当系统需要新功能时,这些新加入的功能仅仅是为了满足一些特定情况下才会执行的特殊行为的需要。这时,装饰器模式提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,但需要执行特殊行为时,客户端代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。
  • 装饰器模式把类中地装饰功能从类中移除,简化了原有的类。
  • 有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网