PHP
  Home arrow PHP arrow Page 2 - 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 - Introducing the basics of the composite pattern


    (Page 2 of 4 )

    In accordance with the concepts that I deployed in the beginning, you'll certainly recall that when the composite pattern is implemented, one specific object, or a set of them, expose an identical behavior across a given application. Even though this definition seems apparently complex, it must be admitted that translating it to functional PHP code is in fact much simpler than you might think.

    Therefore, I'm going to explain how this pattern works by building an abstract PHP class, which as you'll see in a few moments, will define the generic behavior for all the child objects created from it. Logically, in accordance with the schema established by the composite pattern, one or a group of objects that belong to this class will behave similarly in the context of a PHP application.

    Having said that, here is the basic structure corresponding to the aforementioned abstract class:   

    // define abstract 'FileInfoReader' class
    abstract class FileInfoReader{
       // get file info
       abstract public function getSelectedFileInfo(
    $singleFileReader);
       // get number of files
       abstract public function getNumberOfFileReaders();
       // add new file
       abstract public function addFileReader($singleFileReader);
    }

    As you can see, all that I did above was define a simple abstract PHP class, which exposes some generic methods for retrieving information on a given file, as well as for getting and adding a few file reader objects.

    Also, it's worthwhile to mention here that you shouldn't worry for the moment about how these objects will look, since their corresponding definition will be shown in the next section. Now, and returning to the signature of the previous abstract "FileInfoReader" class, you'll see that it presents a special structure. It has to be the model for creating objects that will behave similarly, whether a particular application uses one or a group of them. Sounds really interesting, right?

    But the question that comes up here is: how can this be achieved with PHP? Well, to be frank, the process is quite straightforward and will be limited to deriving two subclasses from the previous parent.

    In this case, the first child class will define the structure of one file reader, while the second one will set up the signature for multiple file readers, in this way implementing the schema dictated by the composite design pattern.

    Want to see how these brand new subclasses will be defined? Okay, go ahead and read the following section.

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