PHP
  Home arrow PHP arrow Page 4 - 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 - 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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