Introducing Visitor Objects in PHP 5 - A visitor object in action
(Page 5 of 5 )
As I mentioned earlier, correctly understanding the behavior of a visitor object is a process that relies mostly on practical examples. That's exactly the reason why I coded a short script (shown below) which demonstrates the functionality of the visitor pattern. Look at the following code listing:
try{
// instantiate 'VisitedArray' object
$visitedArray=new VisitedArray();
// add some elements to the array
$visitedArray->addElement('Element 1');
$visitedArray->addElement('Element 2');
$visitedArray->addElement('Element 3');
$visitedArray->addElement('Element 4');
$visitedArray->addElement('Element 5');
// instantiate 'DataVisitator' object
$dataVisitator=new DataVisitator();
// accept visitor object
$visitedArray->acceptVisitator($dataVisitator);
$dataVisitator->visitArray($visitedArray);
// display info about visited array
echo '<p>Visitor object determined the following information
about visited array:</p>';
echo $dataVisitator->getArrayInfo();
// instantiate 'VisitedFile' object
$visitedFile=new VisitedFile();
// add some lines to the file
$visitedFile->addLine('This is line 1');
$visitedFile->addLine('This is line 2');
$visitedFile->addLine('This is line 3');
$visitedFile->addLine('This is line 4');
$visitedFile->addLine('This is line 5');
// accept visitor object
$visitedFile->acceptVisitator($dataVisitator);
$dataVisitator->visitFile($visitedFile);
// display info about visited file
echo '<p>Visitor object determined the following information
about visited file:</p>';
echo $dataVisitator->getFileInfo();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
Although the above script may seem simple at first glance, it shows in a nutshell the functionality of a visitor object. Notice how this object retrieves the size of the visited array, after populating it with some data, as well as how it obtains the size of the visited file once some strings have been added to this file.
Of course, the output generated by the previous script is as following:
Visitor object determined the following information about visited
array:
Visited array contains the following number of elements: 5
Visitor object determined the following information about visited
file:
Visited file has a size of 600 bytes, and contains 5 lines
Wasn't that great? After the corresponding visited classes neatly allowed the visitor object to inspect their properties, the corresponding information is outputted to the browser. Just let your mind go one step further and think about the implementation of a visitor object capable of inspecting all the visible properties of any visited classes. Here you have the foundations for doing that!
Wrapping up
Unfortunately, we've come to the end of the initial part of our discussion. In this first article of the series, I walked you through the basics of implementing the visitor pattern with PHP 5. I hope that all the code samples that you saw here will serve as an introduction to applying this pattern in a more sophisticated environment. From this moment on, it's up to you.
In the second tutorial, I'll show you more practical examples of how to apply the visitor pattern with PHP 5. Stay tuned!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |