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

Seeing the composite pattern in action - 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

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: