Working with Strings and the Composite Pattern in PHP 5

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.

Introduction

Before I move forward and tell you about the topics that I plan to cover in this final installment of the series, first I’d like to revisit briefly the concepts deployed in the preceding article. This will give you a better idea of how both tutorials are linked to each other.

As you’ll certainly remember, in the first part of the series I introduced the foundations of the composite pattern, and also explained in detail how it could be applied. To quickly review its theoretical definition, in the composite pattern, one object or a set of objects will behave similarly across an application, regardless of the context where they will be used.

Of course, bearing in mind that the previous definition is in fact quite confusing, I demonstrated the application of the pattern in question by setting up a testing example. The example showed how two different file reading classes were able to generate nearly identical outputs, regardless of whether they used one or multiple file handling objects.   

However, I believe that the example that I mentioned previously isn’t good enough to give a reader a decent background on how this pattern works. Thus, since this series is aimed at mastering the basic concepts of the composite pattern, in this final article I’m going to demonstrate how to use it to handle strings of data.

By the end of this article, you should have a more solid foundation on how to apply the pattern in question as part of your own PHP applications.

With the preliminaries out of the way, let’s continue learning more about the composite pattern. Let’s get going!

{mospagebreak title=Handling strings of data}

In consonance with the objective of this tutorial, I’m going to build an abstract class. The class will define a generic model for creating some simple string processor objects.

Later on, based on the structure provided by this base class, I’m going to derive from it a pair of subclasses. Despite their different implementations, these subclasses will expose the same behavior when used within the context of a given PHP application.

As you may have noticed, this is exactly the intrinsic definition of the composite pattern. Having said that, please pay attention to the definition of the abstract class, shown below:    

// define abstract ‘StringProcessor’ class
abstract class StringProcessor{
   // get information about selected string
   abstract public function getSelectedStringInfo(
$singleStringProcessor);
   // get number of string processors
   abstract public function getNumberOfStringProcessors();
   // add new string processor
   abstract public function addStringProcessor(
$singleStringProcessor);
}

As you can see, the above “StringProcessor” class presents a few generic methods that obviously will come in handy for performing some useful tasks. These tasks include getting information about a specific string of data, and counting and adding new string processor objects, which are inputted straight into the class via some of these methods.

However, don’t worry for the moment about how all these string processors will be defined, since I’ll be demonstrating how they look in the section to come.

All right, at this stage I simply created the abstract class that you saw before, but maybe you’re wondering…how does this fit into the schema imposed by the composite pattern?

Good question! Fortunately, the answer is quite simple. Since the composite pattern states clearly that one object or a group of objects will expose the same behavior across a given application, in the next few lines I’m going to create two subclasses from the base “StringProcessor.”

As you’ll see, these child classes will behave identically, in this way implementing the programmatic model dictated by the aforementioned pattern. Quite simple, isn’t it?

Okay, at this moment I’m pretty sure that you want to learn how these subclasses will be created, so click on the link that appears below and keep reading.

{mospagebreak title=Defining the SingleStringProcessor and MultipleStringProcessor classes}

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.

{mospagebreak title=Putting all the classes to work together}

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!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye