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? 
Google.com  
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

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek