PHP
  Home arrow PHP arrow Page 3 - Introducing the Composite Pattern in P...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this first isntallment of the series, you'll learn the key concepts on the...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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