Home arrow PHP arrow Page 4 - An Introduction to Using the Decorator Pattern with PHP

Creating additional decorator classes - PHP

A decorator class allows you to add more capacity to an existing class while leaving the original class untouched. It has certain advantages over inheritance, as you will learn in this first article of a three-part series.

TABLE OF CONTENTS:
  1. An Introduction to Using the Decorator Pattern with PHP
  2. Creating a base class
  3. Defining the structure of a Decorator object
  4. Creating additional decorator classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 16
August 28, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> 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: