试图在php中实现(理解)Decorator模式 [英] Trying to implement (understand) the Decorator pattern in php

查看:89
本文介绍了试图在php中实现(理解)Decorator模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解Decorator模式,并且已经在SO上阅读了其他相关的问题,然后我决定尝试一个简单的例子(我是PHP新手):

I'm trying to understand the Decorator pattern and I have read other related questions on SO, then I decided to try it with a trivial example (I'm a PHP novice):

interface iTitle {
   public function getTitle();
}

class Title implements iTitle {
   protected $_text;

   public function __construct() {
      $this->_text='Our page';
   }

   public function getTitle() {
      return $this->_text;
   }
}

abstract class TitleDecorator implements iTitle {
   protected $_title;

   public function __construct(iTitle $title) {
      $this->_title=$title;
   }
}

class BeforeTitle extends TitleDecorator {
   public function getTitle() {
      return 'Welcome to '.$this->_title->getTitle();
   }
}

class AfterTitle extends TitleDecorator {
   public function getTitle() {
      return $this->_title->getTitle().', Dear user!';
   }
}

这是一种(一种)正确的装饰图案?
如果没有,什么是正确的方式?
如果是的话,这可以改进吗?
或者也许这更适合另一种模式?

Is this a (kind of) right implementation of the Decorator pattern? If no, what would be the right way? If yes, could this be improved and how? Or maybe this would be more suitable for another pattern?

任何帮助/想法将不胜感激,谢谢提前!

Any help/ideas would be appreciated, thanks in advance !

推荐答案


这是Decorator模式的(一种)正确的实现吗?

Is this a (kind of) right implementation of the Decorator pattern?


如果是,可以这样会好转吗?或者也许这更适合另一种模式?

If yes, could this be improved and how? Or maybe this would be more suitable for another pattern?

你实现了Decorator Pattern。我不能想象,当你想实现一个更好的模式时,装饰器模式可以是一个更好的模式。

Well you implemented the Decorator Pattern. I can not imagine what can be a better pattern than the decorator pattern when you want to implement one.

通常情况下,使用Decorator来实现一个接口,一小部分原件反对并传递其余的。这在很大程度上取决于对象的使用。由于您的代码仅显示如何实现模式,而不是使用对象,如果该模式符合您的需求,则无法专门回答。

Normally one would use the Decorator to implement an interface and to replace only a fraction of the originals object and pass the rest along. That heavily depends on object usage. As your code shows only how to implement the pattern but not the use of the objects, it can not be specifically answered if or not that pattern fits your needs.

所以你也可以实现其他模式,然后决定哪一个更好。模式的关键是:只有知道哪个存在,才可以使用它们。

So you can implement other patterns as well and then decide on which one feels better. The crux with patterns is: Only if you know which exists, you can make use of them.

所以在模式和决定中,选择哪一个往往是< a href =http://en.wikipedia.org/wiki/Object-oriented_analysis_and_design =nofollow> OOAD 。如果您刚开始,结帐这本好书

So next to patterns and the decision which one to choose there often is OOAD. If you're new to this, checkout this nice book.

当您询问是否正确时,我首先回答了是。这是正确的,但是这个部分对我来说看起来错了:

I first answered yes when you asked about if that is right or not. Kind of it's right, but this part does looks wrong to me on a second sight:

class BeforeTitle extends TitleDecorator {
   public function getTitle() {
      return 'Welcome to '.$this->getTitle();
   }
}

这是错误的,因为类中的getter BeforeTitle 自动递归调用(它一次又一次地调用...)。我认为这不是你的意图。

It's wrong because the getter in the class BeforeTitle calls itself recursively (it calls itself again and again and again ...). I think that's not intended by you.

你在其他装饰器类中做的正确,参考 $ this-> _title 它是被装饰的对象实例:

You do it correct in the other decorator class, referring to $this->_title which is the object instance that gets decorated:

class AfterTitle extends TitleDecorator {
   public function getTitle() {
      return $this->_title->getTitle().', Dear user!';
   }
}

如果要替换,可以使用Decorator模式/扩展一个对象的行为,但是你想要做到这一点,需要完全重写对象的功能。但其他人可以比我更好地描述。

You can use the Decorator pattern if you want to replace / extend an objects behaviour but you want to do it w/o the need to completely rewrite an objects functionality in whole. But others can describe that better than I.

这篇关于试图在php中实现(理解)Decorator模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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