PHP
  Home arrow PHP arrow Page 4 - Introducing the Flyweight Pattern with PHP 5
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

Introducing the Flyweight Pattern with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2007-02-26


    Table of Contents:
  • Introducing the Flyweight Pattern with PHP 5
  • Defining a target class
  • Defining a flyweight factory class
  • Seeing the flyweight pattern in action

  • 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


    Introducing the Flyweight Pattern with PHP 5 - Seeing the flyweight pattern in action
    ( Page 4 of 4 )

    To demonstrate the functionality provided by the flyweight factory class that I built in the prior section, first I’m going to define a generic form generator class for constructing online forms, and then I’m going to develop an example in which all the classes are put to work together.

    As you’ll see shortly, applying the flyweight design pattern will allow us to construct a basic contact form that only contains the three input fields that you saw previously. In accordance with the logic followed by the pattern in question, any attempt to create new form objects will be refused, keeping the creation of objects completely in equilibrium.

    Having said that, here is the signature that corresponds to the form generator class:  

    // define 'FormGenerator' class
    class FormGenerator{
       private $method='post';
       private $action='processform.php';
       private $formElements=array();
       public function addFormElement(TextInputBox $formElement){
         $this->formElements[]=$formElement;
       }
       // fetch (X)HTML markup of web form
       public function displayForm(){
         $html='<form method="'.$this->method.'" action="'.$this-
    >action.'">';
         foreach($this->formElements as $formElement){
           $html.=$formElement->getHTML().'<br />';
         }
         $html.='<input type="submit" value="Send Data" /></form>';
         return $html;
     
      }
    }

    Now that you know how the above form generator class has been defined, have a look at the following example, which builds a simple contact form by using the flyweight factory class. As you may guess, this form is comprised of only the three basic fields that I explained previously:

     // example building a web form using the Flyweight design pattern
    try{
       // instantiate 'FlyweightFormElementFactory' object
       $flyweightFormElemFactory=new FlyweightFormElementFactory();
       // instantiate input text boxes
       $flyweightNameBox=$flyweightFormElemFactory->fetchFormElement
    ('name');
       $flyweightAddressBox=$flyweightFormElemFactory-
    >fetchFormElement('address');
       $flyweightEmailBox=$flyweightFormElemFactory->fetchFormElement
    ('email');      
       // instantiate 'FormGenerator' object
       $formGenerator=new FormGenerator();
       // add input text objects to form generator
       $formGenerator->addFormElement($flyweightNameBox);
       $formGenerator->addFormElement($flyweightAddressBox);
       $formGenerator->addFormElement($flyweightEmailBox);
       echo $formGenerator->displayForm();
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As shown above, the sample contact form is built by utilizing the functionality of the previous flyweight factory class, in combination with the form generator. Logically, this process is quite simple to follow, and certainly there’s nothing special about it.

    However, see what happens when I try to instantiate a new text input box, aside from the ones allowed by the flyweight factory class:

    // try instantiating a different input text box (triggers an
    exception)
    // $flyweightNameBox1=$flyweightFormElemFactory->fetchFormElement
    ('postalcode');

    In this case the previous script throws an exception and the program’s execution is halted. In addition, here’s another situation where I try to create two input boxes called “name.” In this case, the script returns two identical objects, as shown below:

    // instantiate two identical 'name' input boxes
    $flyweightNameBox1=$flyweightFormElemFactory->fetchFormElement
    ('name');
    $flyweightNameBox2=$flyweightFormElemFactory->fetchFormElement
    ('name');
    if($flyweightNameBox1===$flyweightNameBox2){
      throw new Exception('Input text boxes objects are the same!');
    }

    Now, it should be pretty clear to you how the flyweight pattern works, since in the above example only three instances of the respective “InputTextBox” class are allowed. Any other attempts to create different objects are simply rejected by the flyweight factory class.

    Now, do you realize how a PHP application can control the instantiation of classes via this useful pattern? I bet you do!

    To wrap up

    In this first installment of the series, I introduced the key concepts of the flyweight design pattern, and showed you how to apply them in the context of a form generator application.

    In the course of the final part of the series, I’m going to teach you how to apply this pattern to develop a web page generator system. I don’t think you’ll want to miss it!



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek