php设计模式之装饰器模式_PHP教程

WBOY
Freigeben: 2016-07-13 09:45:53
Original
1001 Leute haben es durchsucht

php设计模式之装饰器模式

1.介绍

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

2.实例

接下来我们举一个例子,使用php实现一个小画板的功能(画指定颜色图形)

1.没使用装饰器之前的传统方式

1.实现一个画板的类

<code class="language-php" hljs="">
<!--?php

    class Canvas
    {
        //保存点阵的一个数组
        public $data;

        //初始化点阵
        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 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 ] = &#39;  &#39;;
                }
            }
        }

        //开始执行画图
        function draw ()
        {
            foreach ($this->data as $line) {
                foreach ($line as $char) {
                    echo $char;
                }
                echo 

;
            }
        }
    }</code>
Nach dem Login kopieren

2.调用

<code class="language-php" hljs="">$canvas = new Canvas();
$canvas->init(40, 20);

$canvas->rect(4,15,9,30);
$canvas->draw();</code>
Nach dem Login kopieren

3.结果 我们看到如下一个正方形

<code class="language-php" hljs="">****************************************
****************************************
****************************************
****************************************
****************************************
*********                      *********
*********                      *********
*********                      *********
*********                      *********
*********                      *********
*********                      *********
*********                      *********
*********                      *********
*********                      *********
*********                      *********
****************************************
****************************************
****************************************
****************************************
****************************************</code>
Nach dem Login kopieren

4.下面问题来了

如果我想给这个图形添加自己想要的颜色怎么添加?

5.解决以上的问题(在显示的时候输出html代码,修改draw方法你就可以看到你想要的结果了)

<code class="language-php" hljs="">        //开始执行画图
        function draw ()
        {
            echo </code>
Nach dem Login kopieren
; foreach ($this->data as $line) { foreach ($line as $char) { echo $char; } echo<br /> ; } echo
; }

6.问题又来了

1.这样写硬编码了,如果那天我不想加颜色了,我想加粗,倾斜,那就需要修改代码,或者我想在上面添加一个标题。。。等等需求,下面我们就用装饰器模式来修改上面的代码,使上面的代码解耦!

2.使用装饰器模式实现上面的功能

1.实现一个装饰器的基类

<code class="language-php" hljs="">//画图装饰器
interface DrawDecorator
{
    //画之前的操作
    function beforeDraw();

    //画之后的操作
    function afterDraw();
}</code>
Nach dem Login kopieren

2.实现一个颜色装饰器

<code class="language-php" hljs="">class ColorDrawDecorator implements DrawDecorator
{
    //颜色属性
    protected $color;

    //初始化颜色
    function __construct($color = &#39;red&#39;)
    {
        $this->color = $color;
    }

    //画之前的操作
    function beforeDraw()
    {
        echo </code>
Nach dem Login kopieren
; } //画之后的操作 function afterDraw() { echo
; } }

3.从新实现画板的类

<code class="language-php" hljs="">
    class Canvas
    {
        //保存点阵的一个数组
        public $data;

        //保存装饰器对象
        protected $decorators = array();

        //初始化点阵
        function init($width = 20, $height = 10)
        {
            $data = array();
            for($i = 0; $i < $height; $i++)
            {
                for($j = 0; $j < $width; $j++)
                {
                    $data[$i][$j] = &#39;*&#39;;
                }
            }
            $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 

;
            }
            $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] = &#39; &#39;;
                }
            }
        }
    }</code>
Nach dem Login kopieren

4.调用

<code class="language-php" hljs="">$canvas = new Canvas();
//注入装饰器对象
$canvas->addDecorator(new ColorDrawDecorator(&#39;green&#39;));
$canvas->init(40, 20);

$canvas->rect(4,15,9,30);
$canvas->draw();</code>
Nach dem Login kopieren

5.结果

输出一个绿色的矩形

同样如果你还想使用加粗,倾斜,设置自定义标题等等,就在创建一个特定的装饰器,注入到画布内就可以实现了

6.总结

1.装饰器就是在执行特定操作之前,加入你自定义的一些操作
2.装饰器的实现,好比钩子(hook)的机制, 比如drupal中的hook机制
3.使用call_user_func 或者 call_user_func_array 也可实现该装饰器机制,这个可以参考drupal的hook实现,也挺不错的!这里先不介绍了,不在设计模式之内,回头有时间在写一下。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1036924.htmlTechArticlephp设计模式之装饰器模式 1.介绍 1.装饰器模式(Decorator),可以动态地添加修改类的功能 2.一个类提供了一项功能,如果要在修改并添加额...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!