PHP
  Home arrow PHP arrow Page 4 - 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 - Seeing the composite pattern in action
    ( Page 4 of 4 )

    In order to demonstrate the functionality provided by the composite pattern, I'm going to develop a testing script, which hopefully will show that the two subclasses defined in the previous section will behave similarly, regardless of the context where they'll be used.

    Given that, and provided that there are three sample files, called "sample_file1.txt", "sample_file2.txt" and "sample_file3.txt" respectively, whose contents are as follows:

    (signature for sample_file1.txt)

    This is content of sample file 1

     

    (signature for sample_file2.txt)

    This is content of sample file 2

     

    (signature for sample_file2.txt)

    This is content of sample file 3

    Here is the sample script in question, which includes the corresponding outputs generated by all the prior classes. Study the following code listing, please:

    try{
       $fileReader1=new SingleFileInfoReader('sample_file1.txt');
       echo $fileReader1->getSelectedFileInfo(1).'<br />';
       /*
       displays the following
       Selected file has the following name: sample_file1.txt and
    its size is: 32 bytes
       */
       $fileReader2=new SingleFileInfoReader('sample_file2.txt');
       echo $fileReader2->getSelectedFileInfo(1).'<br />';
       /*
       displays the following
       Selected file has the following name: sample_file2.txt and
    its size is: 32 bytes
       */
       $fileReader3=new SingleFileInfoReader('sample_file3.txt');
       echo $fileReader3->getSelectedFileInfo(1).'<br />';
       /*
       displays the following
       Selected file has the following name: sample_file3.txt and
    its size is: 32 bytes
       */
       // instantiate 'MultipleFileInfoReader' class
       $fileReaders=new MultipleFileInfoReader();
       // add new file info reader
       $fileReaders->addFileReader($fileReader1);
       echo $fileReaders->getSelectedFileInfo($fileReaders-
    >getNumberOfFileReaders()-1).'<br />';
       /*
       displays the following
       Selected file has the following name: sample_file1.txt and
    its size is: 32 bytes
       */
       // add new file info reader
       $fileReaders->addFileReader($fileReader2);
       echo $fileReaders->getSelectedFileInfo($fileReaders-
    >getNumberOfFileReaders()-1).'<br />';
       /*
       displays the following
       Selected file has the following name: sample_file2.txt and
    its size is: 32 bytes
       */
       // add new file info reader
       $fileReaders->addFileReader($fileReader3);
       echo $fileReaders->getSelectedFileInfo($fileReaders-
    >getNumberOfFileReaders()-1).'<br />';
       /*
       displays the following
       Selected file has the following name: sample_file3.txt and
    its size is: 32 bytes
       */
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the above example clearly illustrates how the two subclasses defined before behave nearly identically, regardless of whether one or a group of file reader objects are used. This circumstance is particularly notable when using the "MultipleFileInfoReader" class, since it displays similar outputs to the ones produced by one file reader. Quite good, right?

    To wrap up

    In this first tutorial of the series, I introduced the basic concepts for applying the composite pattern in PHP 5. As usual with all my articles, I provided you with some examples that should give you an accurate idea of how this pattern works.

    Nonetheless, this journey hasn't ended, since in the last installment I'm going to show you how to implement this pattern along with some string handling classes. I hope to see you there!



     
     
    >>> 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 5 hosted by Hostway
    Stay green...Green IT