note 86026 added to language.oop5.patterns

From: Date: Tue, 30 Sep 2008 00:44:59 +0000
Subject: note 86026 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-144892@lists.php.net to get a copy of this message
Let me describe another pattern it is called “Decorator”. It can be used to add features to already written object. It is useful for extending some library classes from your framework without changing the original classes (especially if somebody else wrote those). The actual meaning is best presented in an example. <?php // the main interface in which we going to do some decoration interface salad{ public function wait(); } // main class which is already defined and you can't or don't want to change class shopSalad implements salad{ private $tomatoes; private $cucumbers; private $cheese; public function __construct($tomatoes, $cucumbers, $cheese){ $this->tomatoes = $tomatoes; $this->cucumbers = $cucumbers; $this->cheese = $cheese; } public function wait(){ echo "the salad is served using {$this->tomatoes} tomatoes, {$this->cucumbers} cucumbers and {$this->cheese} gr. of cheese."; } } // abstract decorator class - note that it implements salad abstract class saladDecorator implements salad{ protected $salad; // the object for decoration public function __construct(salad $salad){ $this->salad=$salad; } // there is no need to mention the wait function therefore this class inherits the original salad // the purpose of this class is maintaining the type of the object produced by the decorators. // it inherits the same interface as the original and it will be inherited by the concrete decorators } // sauce decorator class saladSauce extends saladDecorator { public function wait(){ $this->salad->wait(); echo " Then it was decorated with some sauce."; } } // onions decorator class saladOnions extends saladDecorator { public function wait(){ $this->salad->wait(); echo " Then it was decorated with some onions."; } } $order1 = new shopSalad(2,1,100); // creates the salad in the normal way $order1 = new saladSauce($order1); // add a decorator to the class $order1->wait(); echo '<br />'; // or $order3 = new saladOnions(new saladSauce(new shopSalad(1,0.5,50))); // you don't need to load a variable with the reference $order3->wait(); ?> this will outputs: the salad is served using 2 tomatoes, 1 cucumbers and 100 gr. of cheese. Then it was decorated with some sauce. the salad is served using 3 tomatoes, 2 cucumbers and 100 gr. of cheese. Then it was decorated with some onions. Then it was decorated with some sauce. the salad is served using 1 tomatoes, 0.5 cucumbers and 50 gr. of cheese. Then it was decorated with some souse. Then it was decorated with some onions. The pattern is not intended to be used with simple object and I couldn't do more simple than that. The pattern is useful in every way when the extending of an object is required. It can be used to divide some methods from others or to create layers of interaction with the object. In example if you have an file interface and a imageFile class which implements the file and then you can have also an textFile for the textFile you can do some layering and if you want to read the file as XML you can mace a decorator to do so. The XML decorator will inherit the interface file so you can save it and the instance of textFile will be given to create the decorator so you can read the actual text and pars it as XML. It is possible the file to be .ini or some other text based document and you can make a decorator for every one of them. This is the actual benefit of the pattern. ---- Server IP: 217.75.128.29 Probable Submitter: 79.100.174.216 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/86026 Del: integrated -- https://master.php.net/note/delete/86026/integrated Del: useless -- https://master.php.net/note/delete/86026/useless Del: bad code -- https://master.php.net/note/delete/86026/bad+code Del: spam -- https://master.php.net/note/delete/86026/spam Del: non-english -- https://master.php.net/note/delete/86026/non-english Del: in docs -- https://master.php.net/note/delete/86026/in+docs Del: other reasons-- https://master.php.net/note/delete/86026 Reject -- https://master.php.net/note/reject/86026 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144892) next »