PHP
  Home arrow PHP arrow Page 3 - Working with Strings and the Composite Pattern in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working with Strings and the Composite Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2007-03-14


    Table of Contents:
  • Working with Strings and the Composite Pattern in PHP 5
  • Handling strings of data
  • Defining the SingleStringProcessor and MultipleStringProcessor classes
  • Putting all the classes to work together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Working with Strings and the Composite Pattern in PHP 5 - Defining the SingleStringProcessor and MultipleStringProcessor classes
    ( Page 3 of 4 )

    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.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT