Home arrow PHP arrow Page 4 - Working with Strings and the Composite Pattern in PHP 5

Putting all the classes to work together - 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.

TABLE OF CONTENTS:
  1. Working with Strings and the Composite Pattern in PHP 5
  2. Handling strings of data
  3. Defining the SingleStringProcessor and MultipleStringProcessor classes
  4. Putting all the classes to work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 14, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: