在PHP应用程序中使用Decorator模式 [英] Using the Decorator Pattern in a PHP application

查看:72
本文介绍了在PHP应用程序中使用Decorator模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我的模型似乎出现了很多表示逻辑:

I am getting to a point in my application where there seems to be a lot of presentation logic in my models:

<?php foreach ($this->users as $user): ?>
    <span class="phone">
        <?php echo $user->getPhoneNumberFormattedAsText(); ?>
    </span>
<?php endforeach; ?>

起初,由于需要View Helpers,我开始着手解决这个问题:

At first, I started approaching this as a need for View Helpers:

<span class="phone"><?php echo $this->userPhone($user->getPhone()); ?></span>

但是,我开始遇到一个问题,我有很多特定于某些模型的小View Helper,不需要占用整个文件.如果可以将这种表示逻辑组合在一起并将其排除在模型之外,那就太好了.我认为这是装饰器模式有意义的时候.

However, I've started running into a problem where I have lots of little View Helpers that are specific to certain models, that don't need to take up an entire file. It would be nice if I could group this presentation logic together and keep it out of the model. I think this is when the decorator pattern makes sense.

装饰器模式是一种设计模式,允许动态地将行为添加到现有对象."

"The decorator pattern is a design pattern that allows behaviour to be added to an existing object dynamically."

我在网上看到了一些示例,但没有实际的实用代码示例.我想知道您是否已经在您的PHP应用程序中成功使用了这种模式,以及这种模式的PHP示例如何.

I have seen a few examples online, but no real, practical examples of code. I would like to know if you have successfully used this pattern in your PHP application and what a PHP example of this should look like.

推荐答案

我已经为我的php应用程序实现了一个装饰器模式.基本上,它是xml配置文件的包装器类,在其中定义了我的基本需求.为了简化,我以比萨饼为例.然后,我为每种成分开设一堂课,然后将其包裹在另一堂课中.最后,我调用了每个类的奖赏方法,它给了我所有东西的总和.

I've implemented a decorator pattern for my php application. Basically it's a wrapper class to a xml configuration file where I define my basic needs. To simplify I use a pizza as an example. Then I have a class for each ingredient and wrap it around the other class. In the end I call the prize method of each class and it gives me the sum of everything.

$order = new pizza($xml_file);
$order = new add_salami($order);
$order = new add_cheese($order);
$order = new add_tomato($order);   
$order = $order->prize();

您需要在每个成分类中维护一个指向当前对象的指针.当您调用php new函数时,可以使用它来备份当前对象.这有点像(对象的)链表.然后,您可以调用最后一个对象的award()方法并遍历所有其他类.但是要装饰,您需要添加新的类.您也可以将$ xml_file替换为起始值.我所有的装饰器类都放在一个文件中.装饰器类可以如下所示:

You need to maintain a pointer to the current object in every ingredient class. When you call the php new function you can use it to backup the current object. It's a bit like a linked list (of objects). Then you can call the prize() method of the last object and loop through all other classes. But to decorate you need to add new classes. You can also replace the $xml_file with a start value. I've all my decorator class in one file. A decorator class can looks like this:

class add_salami {

    protected $order;
    protected $prize;

    public function __construct ($order) {
        $this->order = $order;
        $this->prize = $order->getPrize();
    }

    public function getPrize() {
        return $this->prize + 10;
    }
}

我将许多微型装饰器类保存在一个大文件中.

I keep many of these tiny decorator classes in a huge file.

这篇关于在PHP应用程序中使用Decorator模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆