Tag Archive for 'php object traversal'

PHP Visitor Design Pattern III: Traverser

phper250After going through single and double dispatch, it’s time to shift gears to the next major feature of the Visitor pattern; the Traverser. One of the most important (and often omitted) components of the Visitor design pattern is the Object Structure. In Part II the focus is on the double-dispatch, the number of elements that could be visited at once. The Object Structure is set up to collect the allowable visitors to visit the elements; however, only one element at at time can be displayed because the middle of the the foreach loop in the confirm() method contains a return statement—so as soon a the iteration begins, it ends with the return statement. (Let’s face it; it’s a pseudo-transveral.) Since Part II focuses on the double-dispatch, the number of elements that could be visited at one time is a tangental issue. This post, though, focuses squarely on the traversal process and the multiple elements that can be visited in a single process.

Updating the Visitor

In order to have all of the parts merrily humming along, a few changes are in order. Importantly, once these changes are made, all of the parts are still working and interacting together. To see what’s changed but how the structure is basically the same as the visitor in Part II focuses on the double-dispatch, the number of elements that could be visited at on, download the new set of files and test the new multiple element visitor:
Right away, you will see that instead of radio buttons, the HTML5 UI uses check boxes for selecting one, two or three elements and then applies the visitors one at a time.(You can supply your own error-handling routine for selecting no elements!)

The HTML Element Array

One of the nicest attributes in HTML is the ability to treat a group of checkboxes with “element values” (elements as elements in an array). Each checkbox is given a name with empty brackets (e.g., my element[]). All of the selected (checked) checkboxes with the identical element name are automatically included in an array with the element name. You don’t have to worry about the unchecked ones as they are not included in the array passed to PHP.

< !DOCTYPE html>
    <link rel="stylesheet" type="text/css" href="visitor.css"/>
    <title>The Traverser</title>
    <h3>Traverser: Multiple Elements & Visitor Color</h3>
    <div id="choose">
        <div id="geom">
    <form action="Client.php" method = "post" target = "feedback">
        <input type="checkbox" name="shape[]" value="Circle" checked="True"/>&nbsp;Dispatch a circle<br />
        <input type="checkbox" name="shape[]" value="Square"/>&nbsp;Dispatch a square<br />
        <input type="checkbox" name="shape[]" value="Triangle"/>&nbsp;Dispatch a triangle<br />
                <legend>Color Visitor</legend>
        <input type="radio" name="color" value="GreenVisitor" checked="True"/>&nbsp;Green<br />
        <input type="radio" name="color" value="RedVisitor"/>&nbsp;Red<br />
        <input type="radio" name="color" value="BlueVisitor"/>&nbsp;Blue<br />
        <input type="radio" name="color" value="BurntOrangeVisitor"/>&nbsp;Burnt Orange<br />
        <p><input type="submit" name="sender" value="Double Dispatch"/></p>
    <iframe name="feedback" width="680" height="680">feedback</iframe>

That checkbox array feature made it very easy to pass the shape class names (as strings) to the Client.

The Updated Client

The Client class is changed slightly to accept an array of shapes rather than just one as was done in Parts I and II of the Visitor series. Likewise, some steps were tightened up in the ObjectStructure class for cleaning up the traversal process.

< ?php
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
// Autoload given function name.
function includeAll($className)
    include_once($className . '.php');
class Client
    private static $shapeElement;
    private static $color;
    private static $package;
    //client request
    public static function request()
      self::$shapeElement= array();
      self::$package= array();
      $obStructure = new ObjectStructure();
      $colorVisitor= new self::$color();
      //Attach concrete elements to array & accept visitor
      foreach (self::$shapeElement as $shapeNow)
        $obStructure->attach(new $shapeNow,$colorVisitor);
      //Display selected shapes
      foreach (self::$package as $colorShape)
        echo $colorShape->showShape();

The Client calls on the traversal process using an instance of the OjbectStructure. The attach() method now has two parameters; a shape and a color. The shape is passed as a shape element generated by the foreach loop, creating a new shape with each iteration while adding the same color visitor to all shapes. Once everything has been stored in the ObjectStructure, a second loop calls the getElements() methods, which returns shapes with the appropriate color visitor.

The ObjectStructure

The ObjectStructure class is small but mighty. The Gang of Four note,

A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor.

Several choices are available to transverse the elements from the client through the object structure, but as seen, the humble (yet mighty) foreach loop handles the job elegantly and efficiently. The OjbectStructure provides the methods the client uses for traversal.

< ?php
class ObjectStructure
    private $elements=array();
    public function attach(IElement $element,IVisitor $colorVis)
    public function getElements()
        return $this->elements;

In Part II of the Visitor series, the attach() method only served to push the element onto an array, but here the method, now with two instead of one parameter serves double duty. First, each shape accepts (using the IElement::accept() method) the visitor. Second, each shape is added to an array, $elements. In some respects, this acts like a setter operation.

The only other method, getElements() returns the shapes with the accepted color. The client can then display them on the screen.
Continue reading ‘PHP Visitor Design Pattern III: Traverser’