PHP
  Home arrow PHP arrow Page 3 - Introducing 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

Introducing the Composite Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-03-07


    Table of Contents:
  • Introducing the Composite Pattern in PHP 5
  • Introducing the basics of the composite pattern
  • Implementing the composite pattern's model
  • Seeing the composite pattern in action

  • 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


    Introducing the Composite Pattern in PHP 5 - Implementing the composite pattern's model
    ( Page 3 of 4 )

    As I said in the section that you just read, the next step to take toward the implementation of the composite pattern will be based upon deriving two subclasses from the parent "FileInfoReader" class that you saw previously.

    In this case, the first child class will be responsible for defining the behavior of only one file reader object, while the second subclass will be tasked with modeling the behavior corresponding to multiple file readers. As you'll hopefully recall from the definition of the composite pattern, all these file reader objects must behave nearly identically.

    Okay, now that I have explained how each of the aforementioned subclasses are going to work, please take a look at their respective signatures, which have been included below:

    // define concrete 'SingleFileInfoReader' class
    class SingleFileInfoReader extends FileInfoReader{
       private $fileName;
       public function __construct($fileName){
         if(!file_exists($fileName)){
           throw new Exception('Invalid input file!');
         }
         $this->fileName=$fileName;
       }
       public function getSelectedFileInfo($fileReader){
         if($fileReader==1){
           return 'Selected file has the following name: '.$this-
    >fileName.' and its size is: '.filesize($this->fileName).'
    bytes';
         }
         return false;
       }
       public function getNumberOfFileReaders(){
         return 1;
       }
       public function addFileReader($singleFileReader){
         return false;
       }
    }

    // define concrete 'MultipleFileInfoReader' class
    class MultipleFileInfoReader extends FileInfoReader{
       private $fileReaders;
       public function __construct(){
         $this->fileReaders=array();
       }
       public function getSelectedFileInfo($singleFileReader){
         if($singleFileReader<=count($this->fileReaders)){
           return $this->fileReaders[$singleFileReader]-
    >getSelectedFileInfo(1);
         }
         return false;
       }
       public function getNumberOfFileReaders(){
         return count($this->fileReaders);
       }
       public function addFileReader($singleFileReader){
         $this->fileReaders[]=$singleFileReader;
       }
    }

    As illustrated above, the two subclasses defined previously show in a clear fashion how the composite pattern works. More specifically speaking, in the first case, the "SingleFileInfoReader" class has implemented all the abstract methods declared by the respective parent in such a way that it will return to client code data corresponding to only one file reader.

    With reference to the behavior exposed by the class in question, it's clear to see here that its "getSelectedFileInfo()" method will only retrieve information about a particular file as long as the specified file reader will be equal to 1, while the other two methods will return a value of 1 and FALSE respectively. This is because the latter two methods are tasked with counting and adding new file reader objects, something not applicable in this case, so these responses are quite logical.

    So far, so good. Now, having analyzed the behavior of the first subclass, please focus your attention on the second one, which is the one called "MultipleFileInfoReader." As you can see, this class also implements concretely the same methods defined by the respective parents, which is what you expect from a concrete class.

    However, the most interesting aspects with regard to the methods mentioned rest upon the way that they've been defined. Please notice how the first one, named "getSelectedFileInfo()," will return to calling code data corresponding to a particular file, in this case by using only one file reader object. This implies directly that this "MultipleFileInfoReader" class behaves very similarly to "SingleFileInfoReader".

    Of course, the other two methods, that is "getNumberOfFileReaders()" and "addFileReader()" are really simple to follow and also work as expected, so I won't spend a long time here explaining what they do.

    Now are you starting to see how the two previous subclasses implement the composite pattern? I'm sure you are!

    All right, at this stage you've hopefully grasped the logic that drives the composite pattern, thus assuming that you've properly understood the way that the pair of above subclasses work, it's time to leap forward and see how they can be used together within a sample script.

    Therefore, taking into account this condition, in the following section I'm going to show you how to apply the composite pattern by using the couple of child classes that you learned a few lines above.

    As usual, this sample script will be built in the section to come, so click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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