PHP
  Home arrow PHP arrow Page 4 - An Introduction to Using the Decorator...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

An Introduction to Using the Decorator Pattern with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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,...
       · hihow 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...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT