开发者

Trying to implement (understand) the Decorator pattern in php

I'm trying to understand the Decorator pattern and开发者_JS百科 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 !


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

Yes

If yes, could this be improved and how? Or maybe this would be more suitable for another 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.

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.

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.

About your code

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();
   }
}

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.

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!';
   }
}

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.


In decorator patter you should always inject value not initialize inside constructor. Example : This is wrong way.

class Title implements iTitle {
   protected $_text;

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

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

This is correct way:

 class Title implements iTitle {
   protected $_text;

   public function __construct($text) {
      $this->_text=$text;
   }

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

If you need more detailed real world example see the link here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜