dtkl55257 2012-06-27 18:38
浏览 32

在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; ?>

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

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

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."

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.

  • 写回答

3条回答 默认 最新

  • douti6740 2012-06-27 18:39
    关注

    Zend Framework uses the decorator pattern for its form elements. Imagine having an input element with type="text". You could 'decorate' this element with a label, divs, fieldsets, etc.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型