Defining methods for adding and removing views - PHP
The Composite View design pattern offers users the versatility of handling single objects and collections through the same interface. But some programmers hesitate to use it due to its tricky implementation. Hesitate no longer; this multi-part series will take you step by step through some of the best ways to use the Composite View design pattern, complete with code samples to show you the way.
As I said in the preceding section, it’s necessary to provide the previous abstract parent class with an interface that permits it to add and remove view objects, so that they can be handled either individually or in conjunction. Since this functionality should be added by concrete implementation of the pertinent parent, in this case the interface will be defined by a couple of abstract methods, called “addView()” and “removeView()” respectively.
Here’s how the earlier “AbstractView” class looks after adding these methods:
// constructor public function __construct($template = '', array $data = array()) { if ($template !== '') { $this->setTemplate($template); } if (!empty($data)) { foreach ($data as $name => $value) { $this->$name = $value; } } }
// set a new view template public function setTemplate($template) { $template = $template . '.php'; if (!file_exists($template)) { throw new ViewException('The specified view template does not exist.'); } $this->_template = $template; }
// get the view template public function getTemplate() { return $this->_template; }
// set a new property for the view public function __set($name, $value) { $this->_properties[$name] = $value; }
// get the specified property from the view public function __get($name) { if (!isset($this->_properties[$name])) { throw new ViewException('The requested property is not valid for this view.'); } return $this->_properties[$name]; }
// remove the specified property from the view public function __unset($name) { if (isset($this->_properties[$name])) { unset($this->_properties[$name]); } }
// add a new view (implemented by view subclasses) abstract public function addView(AbstractView $view);
// remove a view (implemented by view subclasses) abstract public function removeView(AbstractView $view); }
There you have it. Even though the brand new “addView()” and “removeView()” methods have been declared abstract, their signatures do allow you to have a more accurate idea of how the Composite View pattern does its thing. The methods accept other view objects for further aggregation or removal, therefore putting in evidence the use of Composition over Inheritance.
If you still don’t have a clue as to how a single view and collections of them can be handled indiscriminately via the same interface, don’t feel concerned. Things will become clear when I show you the definition of the subclasses derived from the previous abstract parent.
The only thing that remains undone now is adding to this parent a method that permits it to render the assigned view template on screen. This will be done in the following section, so go ahead and read the next few lines.