HomePHP Page 3 - Working with Strings and the Composite Pattern in PHP 5
Defining the SingleStringProcessor and MultipleStringProcessor classes - PHP
Are you a PHP developer who wants to improve your skills in pattern-based programming in PHP? If the answer to this question is an emphatic yes, then you should begin reading this article now! Welcome to the final part of the series “Implementing the composite pattern with PHP 5.” Comprised of two instructive tutorials, this series walks you through the basic concepts of this important design pattern, and shows you how to apply it with some educational examples.
To illustrate how the composite pattern can be applied using the abstract string processor class that you learned in the previous section, I'm simply going to derive two subclasses from this base class.
As you’ll see in a moment, these child classes, which are also string processor objects, will be capable of behaving nearly the same, even when they offer different implementations. Indeed, this sounds like I’m sticking to the model imposed by the composite pattern, right?
All right, now that you know how these two subclasses are going to work, please have a look at their respective signatures, which are listed below. Here they are:
// define concrete 'SingleStringProcessor' class class SingleStringProcessor extends StringProcessor{ private $inputString; public function __construct($inputString){ if(!is_string($inputString)){ throw new Exception('Invalid input string!'); } $this->inputString=$inputString; } public function getSelectedStringInfo($stringProcessor){ if($stringProcessor==1){ return 'Value of input string is the following: '.$this- >inputString.' and its length is: '.strlen($this->inputString). 'chars.'; } } public function getNumberOfStringProcessors(){ return 1; } public function addStringProcessor($singleStringProcessor){ return false; } }
// define concrete 'MultipleStringProcessor' class class MultipleStringProcessor extends StringProcessor{ private $stringProcessors; public function __construct(){ $this->stringProcessors=array(); } public function getSelectedStringInfo($singleStringProcessor){ if($singleStringProcessor<=count($this->stringProcessors)){ return $this->stringProcessors[$singleStringProcessor]- >getSelectedStringInfo(1); } return false; } public function getNumberOfStringProcessors(){ return count($this->stringProcessors); } public function addStringProcessor($singleStringProcessor){ $this->stringProcessors[]=$singleStringProcessor; } }
As shown above, both the “SingleStringProcessor” and “MultipleStringProcessor” classes implement all the methods originally declared in the respective base class, which is certainly expected when working with abstract classes. However, aside from this characteristic, I’d like you to pay attention to the definition corresponding to these methods. The definition does show in a clear fashion how this pair of subclasses implement the composite pattern.
Please notice how the methods that belong to the first child class return to calling code a value of 1 or FALSE, since here it’s assumed that this class is only capable of using a single string processor object.
On the other hand, there’s the “MultipleStringProcessor” class, which is tasked with handling multiple string processor objects. Nevertheless, the most important thing to stress here is the fact that both classes will behave similarly, regardless of whether they handle only one or a group of string processors. After all, implementing the composite pattern by using the two prior child classes wasn’t hard at all!
So far, I have shown you the respective signatures corresponding to the “SingleStringProcessor” and “MultipleStringProcessor” classes respectively, but now I’m pretty certain that you want to see how they can be included into a simple script to demonstrate that they do present the same behavior.
Indeed, the experience sounds interesting, thus in the last section of this tutorial I’m going to create an educational example which will use the two child classes that you learned a few lines above.
To see how the example in question will be developed, please go ahead and read the next few lines. I’ll be there, waiting for you.