Home arrow PHP arrow Page 3 - Introducing the Composite Pattern in PHP 5

Implementing the composite pattern's model - PHP

The composite pattern is one of the less commonly used patterns in PHP 5. Nevertheless, in certain situations, it is very helpful. This article, the first one in a two-part series, will introduce you to the basic concepts of the composite pattern.

TABLE OF CONTENTS:
  1. Introducing the Composite Pattern in PHP 5
  2. Introducing the basics of the composite pattern
  3. Implementing the composite pattern's model
  4. Seeing the composite pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
March 07, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: