HomePHP 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.
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!