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) 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 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 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) 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(){ 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 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|