Working with Strings and the Composite Pattern in PHP 5 - Putting all the classes to work together (
Page 4 of 4 )
As I expressed in the previous section, below I included a testing script. It shows quite accurately how the two string processor classes that were defined before can be used together to implement the composite pattern.
In this particular case, the script also shows the corresponding outputs generated by each of the classes, so you can see more clearly how they have similar behaviors.
Having pointed that out, please take a look at the following code listing:
try{
$stringProcessor1=new SingleStringProcessor('This is the first
sample string and will be used in the composite pattern');
echo $stringProcessor1->getSelectedStringInfo(1).'<br />';
/*
displays the following:
Value of input string is the following: This is the first
sample string and will be used in the composite pattern and its
length is: 73chars.
*/
$stringProcessor2=new SingleStringProcessor('This is the
second sample string and will be used in the composite pattern');
echo $stringProcessor2->getSelectedStringInfo(1).'<br />';
/*
displays the following:
Value of input string is the following: This is the second
sample string and will be used in the composite pattern and its
length is: 74chars.
*/
$stringProcessor3=new SingleStringProcessor('This is the third
sample string and will be used in the composite pattern');
echo $stringProcessor3->getSelectedStringInfo(1).'<br />';
/*
displays the following:
Value of input string is the following: This is the third
sample string and will be used in the composite pattern and its
length is: 73chars.
*/
// instantiate 'MultipleStringProcessor' class
$stringProcessors=new MultipleStringProcessor();
// add new string processor
$stringProcessors->addStringProcessor($stringProcessor1);
echo $stringProcessors->getSelectedStringInfo(
$stringProcessors->getNumberOfStringProcessors()-1).'<br />';
/*
displays the following:
Value of input string is the following: This is the first
sample string and will be used in the composite pattern and its
length is: 73chars.
*/
// add new string processor
$stringProcessors->addStringProcessor($stringProcessor2);
echo $stringProcessors->getSelectedStringInfo(
$stringProcessors->getNumberOfStringProcessors()-1).'<br />';
/*
displays the following:
Value of input string is the following: This is the second
sample string and will be used in the composite pattern and its
length is: 74chars.
*/
// add new string processor
$stringProcessors->addStringProcessor($stringProcessor3);
echo $stringProcessors->getSelectedStringInfo(
$stringProcessors->getNumberOfStringProcessors()-1).'<br />';
/*
displays the following:
Value of input string is the following: This is the third
sample string and will be used in the composite pattern and its
length is: 73chars.
*/
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
If you take some time and study the outputs generated by each of the string processor classes used in the previous example, you’ll probably see that they produce the same results, even when they have different implementations. Of course, this means in simple terms that one string processor or a group of them expose the same behavior, in this way completing the programmatic model dictated by the composite pattern.
As usual with all my PHP articles, feel free to modify the source code of the classes shown here, so you can create your own hands-on examples, and eventually acquire a better understanding of how this handy pattern works.
Final thoughts
That’s about it. In the two articles of this series, you’ve hopefully expanded your background in pattern-based programming by adding a new pattern to your list. Of course I’m speaking of the composite pattern, which as you saw establishes a programmatic model where one object or a group of them behave similarly.
See you in the next PHP tutorial!