zoukankan      html  css  js  c++  java
  • php设计模式 装饰器模式

    装饰器模式,可以动态地添加修改类的功能。

    一个类提供了一项功能,如果要修改并添加额外的功能,传统的编程模式需要写一个子类继承它,并重新实现类的方法。使用装饰器模式,仅需要在运行时添加一个装饰器对象即可实现,可以实现最大的灵活性。

    DrawDecorator.php
    <?php
    namespace Baobab;
    /**
     * Interface DrawDecorator
     * @package Baobab
     * 装饰器接口,定义两个方法,渲染画布之前和之后可添加额外的能力
     */
    interface DrawDecorator{
        function beforeDraw();
        function afterDraw();
    }
    Canvas.php
    <?php
    namespace Baobab;
    
    /**
    *Canvas类,绘制图形
    **/
    class Canvas { public $data; protected $decorators = array(); //Decorator function init($width = 20, $height = 10) { $data = array(); for($i = 0; $i < $height; $i++) { for($j = 0; $j < $width; $j++) { $data[$i][$j] = '*'; } } $this->data = $data; } function addDecorator(DrawDecorator $decorator){ $this->decorators[] = $decorator; } function beforeDraw(){ foreach($this->decorators as $decorator){ $decorator->beforeDraw(); } } function afterDraw(){ $decorators = array_reverse($this->decorators); foreach($decorators as $decorator){ $decorator->afterDraw(); } } function draw() { $this->beforeDraw(); foreach($this->data as $line) { foreach($line as $char) { echo $char; } echo "<br /> "; } $this->afterDraw(); } function rect($a1, $a2, $b1, $b2) { foreach($this->data as $k1 => $line) { if ($k1 < $a1 or $k1 > $a2) continue; foreach($line as $k2 => $char) { if ($k2 < $b1 or $k2 > $b2) continue; $this->data[$k1][$k2] = '&nbsp;'; } } } }
    ColorDrawDecorator.php
    <?php
    namespace Baobab;
    /**
    *修改图形颜色的装饰器
    */
    class ColorDrawDecorator implements BaobabDrawDecorator{ protected $color; function __construct($color = 'black'){ $this->color = $color; } function beforeDraw(){ echo "<div style='color: {$this->color};'>"; }
    function afterDraw(){ echo "</div>"; } }
    SizeDrawDecorator.php
    <?php
    namespace Baobab;
    /**
    *修改图形大小的装饰器
    */
    class SizeDrawDecorator implements BaobabDrawDecorator{ protected $size; function __construct($size = '12px'){ $this->size = $size; } function beforeDraw() { echo "<div style='font-size: {$this->size};'>"; }
    function afterDraw() { echo "</div>"; } }

    index.php

    $canvas = new BaobabCanvas();
    $canvas->init();
    $canvas->rect(3, 6,4,12);
    
    $canvas->addDecorator(new BaobabColorDrawDecorator('blue'));
    $canvas->addDecorator(new BaobabSizeDrawDecorator('10px'));
    ......
    //可添加其他更多装饰器
    $canvas->draw();
  • 相关阅读:
    华为机试题 二叉查搜索树 判断两序列是否为同一二叉搜索树序列
    华为机试题 火车进站
    Linux命令学习 rmdir
    Linux命令学习 rm
    Linux命令学习 mkdir
    Linux命令学习 du
    Linux命令学习 pwd
    Linux命令学习 ls
    Linux命令学习 cd
    伪代码编程过程
  • 原文地址:https://www.cnblogs.com/tianxintian22/p/5241647.html
Copyright © 2011-2022 走看看