6.2 Понимание и использование Zend Form Decorators. Основы декораторов

Zend, “Understanding and Using Zend Form Decorators. Decorator Basics”, public translation into Russian from English More about this translation.

See also 44 similar translations

Translate into another language.

Participants

antdmi 9 points
Join Translated.by to translate! If you already have a Translated.by account, please sign in.
If you do not want to register an account, you can sign in with OpenID.
Pages: ← previous Ctrl next next untranslated
1 2 3 4

Understanding and Using Zend Form Decorators. Decorator Basics

6.2 Понимание и использование Zend Form Decorators. Основы декораторов

History of edits (Latest: antdmi 6 years, 5 months ago) §

Overview of the Decorator Pattern

To begin, we'll cover some background on the » Decorator design pattern. One common technique is to define a common interface that both your originating object and decorator will implement; your decorator than accepts the originating object as a dependency, and will either proxy to it or override its methods. Let's put that into code to make it more easily understood:

  01. interface Window

  02. {

  03.     public function isOpen();

  04.     public function open();

  05.     public function close();

  06. }

  07. 

  08. class StandardWindow implements Window

  09. {

  10.     protected $_open = false;

  11. 

  12.     public function isOpen()

  13.     {

  14.         return $this->_open;

  15.     }

  16. 

  17.     public function open()

  18.     {

  19.         if (!$this->_open) {

  20.             $this->_open = true;

  21.         }

  22.     }

  23. 

  24.     public function close()

  25.     {

  26.         if ($this->_open) {

  27.             $this->_open = false;

  28.         }

  29.     }

  30. }

  31. 

  32. class LockedWindow implements Window

  33. {

  34.     protected $_window;

  35. 

  36.     public function __construct(Window $window)

  37.     {

  38.         $this->_window = $window;

  39.         $this->_window->close();

  40.     }

  41. 

  42.     public function isOpen()

  43.     {

  44.         return false;

  45.     }

  46. 

  47.     public function open()

  48.     {

  49.         throw new Exception('Cannot open locked windows');

  50.     }

  51. 

  52.     public function close()

  53.     {

  54.         $this->_window->close();

  55.     }

  56. }

You then create an object of type StandardWindow, pass it to the constructor of LockedWindow, and your window instance now has different behavior. The beauty is that you don't have to implement any sort of "locking" functionality on your standard window class -- the decorator takes care of that for you. In the meantime, you can pass your locked window around as if it were just another window.

Pages: ← previous Ctrl next next untranslated
1 2 3 4