An Introduction to Using the Decorator Pattern with PHP - Creating additional decorator classes (
Page 4 of 4 )
As you learned in the previous section, once the “StringDecorator” class has been properly defined, extending the functionality of the base “StringSaver” class that you saw before is a breeze. All I have to do is derive a pair of simple subclasses from the “StringDecorator” class and the work is almost done.
That said, here are the corresponding definitions for the decorator subclasses, tasked with lowercasing and uppercasing the $str property of the respective base class, but without changing its original structure:
// define 'StringUppercaser' class (decorates 'StringSaver' class)
class StringUppercaseDecorator extends StringDecorator{
private $strDecorator;
// pass 'StringDecorator' object to constructor
public function __construct(StringDecorator $strDecorator){
$this->strDecorator=$strDecorator;
}
// convert string to uppercase
public function uppercaseString(){
$this->strDecorator->str=strtoupper($this->strDecorator-
>str);
}
}
// define 'StringLowercaser' class (decorates 'StringSaver' class)
class StringLowercaseDecorator extends StringDecorator{
private $strDecorator;
// pass 'StringDecorator' object to constructor
public function __construct(StringDecorator $strDecorator){
$this->strDecorator=$strDecorator;
}
// convert string to lowercase
public function lowercaseString(){
$this->strDecorator->str=strtolower($this->strDecorator-
>str);
}
}
As you can see, the two classes listed above are in fact subclasses of “StringDecorator,” and accept this object as the only input parameter. The reason for doing this should now be obvious, since both child classes use their “uppercaseString()” and “lowecaseString()” methods respectively to modify the $str property, while maintaining the structure of the base class clearly untouched.
The following method definitions demonstrate the concepts that I explained previously:
// convert string to uppercase
public function uppercaseString(){
$this->strDecorator->str=strtoupper($this->strDecorator-
>str);
}
// convert string to lowercase
public function lowercaseString(){
$this->strDecorator->str=strtolower($this->strDecorator-
>str);
}
Now you should be able to understand how a decorator class works to expand the capacity of a specific class, without modifying its original definition. Nevertheless, I’d like to show you how all the previous sample classes fit with each other, therefore I’ve set up a demonstrative example below. Have a look, please:
// instantiate 'StringSaver' object
$strSaver=new StringSaver('strings/strings.txt','This is a sample
string');
// instantiate 'StringDecorator' object
$strDecorator=new StringDecorator($strSaver);
// instantiate 'StringUppercaseDecorator' object
$strupperDecorator=new StringUppercaseDecorator($strDecorator);
// instantiate 'StringLowercaseDecorator' object
$strlowerDecorator=new StringLowercaseDecorator($strDecorator);
echo 'Original string is as follows: '.$strDecorator-
>displayString().'<br />';
// convert string to uppercase
$strupperDecorator->uppercaseString();
echo 'String after using 'StringUppercaseDecorator' object is
as follows: '.$strDecorator->displayString().'<br />';
// convert string to lowercase
$strlowerDecorator->lowercaseString();
echo 'String after using 'StringLowercaseDecorator' object is
as follows: '.$strDecorator->displayString().'<br />';
The above code snippet shows how the respective decorator classes can be used, in order to uppercase and lowercase a given string, which has been passed as an argument to the original “StringSaver” class.
Not surprisingly, the output of the previous script is the following:
Original string is as follows: This is a sample string
String after using 'StringUppercaseDecorator' object is as follows: THIS IS A SAMPLE STRING
String after using 'StringLowercaseDecorator' object is as follows: this is a sample string
All right, I believe the examples shown above demonstrate in an easy way how the decorator pattern works. However, l will go one step further with reference to this example and create an additional decorator class, which will be capable of reversing the original input string. Here is the definition for this brand new decorator class:
// define 'StringReverser' class (decorates 'StringSaver' class)
class StringReverseDecorator extends StringDecorator{
private $strDecorator;
// pass 'StringDecorator' object to constructor
public function __construct(StringDecorator $strDecorator){
$this->strDecorator=$strDecorator;
}
// reverse string
public function reverseString(){
$this->strDecorator->str=strrev($this->strDecorator-
>str);
}
}
If you examine the class listed above, you’ll realize that it implements the same approach that you learned with the previous decorator classes. Of course, the only difference rests on the fact that the corresponding $str property is reversed via the “reverseString()” method:
public function reverseString(){
$this->strDecorator->str=strrev($this->strDecorator->str);
}
Now that you know how this newly created class does its business, take a look at how it can be used in conjunction with the other decorator classes:
// instantiate 'StringSaver' object
$strSaver=new StringSaver('strings/strings.txt','This is a sample
string');
// instantiate 'StringDecorator' object
$strDecorator=new StringDecorator($strSaver);
// instantiate 'StringUppercaseDecorator' object
$strupperDecorator=new StringUppercaseDecorator($strDecorator);
// instantiate 'StringLowercaseDecorator' object
$strlowerDecorator=new StringLowercaseDecorator($strDecorator);
// instantiate 'StringReverseDecorator' object
$strrevDecorator=new StringReverseDecorator($strDecorator);
echo 'Original string is as follows: '.$strDecorator->displayString().'<br />';
// convert string to uppercase
$strupperDecorator->uppercaseString();
echo 'String after using 'StringUppercaseDecorator' object is
as follows: '.$strDecorator->displayString().'<br />';
// convert string to lowercase
$strlowerDecorator->lowercaseString();
echo 'String after using 'StringLowercaseDecorator' object is
as follows: '.$strDecorator->displayString().'<br />';
// reverser string
$strrevDecorator->reverseString();
echo 'String after using 'StringReverseDecorator' object is as
follows: '.$strDecorator->displayString().'<br />';
// convert string to uppercase
$strupperDecorator->uppercaseString();
echo 'String after using 'StringUppercaseDecorator' object is
as follows: '.$strDecorator->displayString().'<br />';
And finally, the results outputted by the previous script would be as follows:
Original string is as follows: This is a sample string
String after using 'StringUppercaseDecorator' object is as follows: THIS IS A SAMPLE STRING
String after using 'StringLowercaseDecorator' object is as follows: this is a sample string
String after using 'StringReverseDecorator' object is as follows: gnirts elpmas a si siht
String after using 'StringUppercaseDecorator' object is as follows: GNIRTS ELPMAS A SI SIHT
Try creating different decorator classes and see what happens in each case. The experience is really educational. Believe me, you won’t be disappointed!
Final thoughts
In this first article of the series, you learned the basics of how to apply the decorator pattern in PHP. Although the sample classes that I coded here were quite simple, they allowed me illustrate by an easy fashion the way this pattern can be used.
However, my journey has not finished yet. In the next tutorial, I’ll explain how to use the decorator pattern in a group of classes that handle MySQL result sets, in this way bringing this pattern to a real-world application. If you wish to find out how this will be done, don’t miss the next tutorial!
| | Discuss An Introduction to Using the Decorator Pattern with PHP | | | | | | | Over the course of this first article, you'll learn how to apply the decorator... | | | | | | Very good tutorial. Looking forward to the next in series. | | | | | | Thank you for the kind words on my PHP article. Also, I'm glad to know you liked it,... | | | | | | hi
how does the strupperDecorator object modify the strDecorator object's str... | | | | | | Hi Dave,
With reference to your question, StringDecorator and... | | | | | | it's better to check return of fwrite as well, not only fopen ;) | | | | | | Thank you for the feedback. Yeap, you're correct, but I just wanted to write down... | | | | | | flose($fp); by fclose($fp);
fwrite($fp,$str); by fwrite($fp,$this->str);
thx... | | | | | | Thank you for the useful contributions to my PHP article. I think they'll be... | | | | | | >>> Post your comment now! | | | | | |
|