PHP
  Home arrow PHP arrow Page 4 - An Introduction to Using the Decorator Pattern with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

An Introduction to Using the Decorator Pattern with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-08-28


    Table of Contents:
  • An Introduction to Using the Decorator Pattern with PHP
  • Creating a base class
  • Defining the structure of a Decorator object
  • Creating additional decorator classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek