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

Introducing the Flyweight Pattern with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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


    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!


    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.

       · If you want to learn now to keep under control the number of objects created by a...
       · HiFirst of all I would like to say that you have written a nice article(short...
       · Hi Daniel,Firstly, I’d like to thank you for your kind comments on my PHP...
       · I'm sorry, your concept of "Flyweight" is not correct. Please re-read your Gang Of...
       · Thank you for commenting on my PHP article. Unfortunately, I won’t be able to reread...
       · FlyweightIntentUse sharing to support large numbers of fine-grained objects...
     

       

    PHP ARTICLES

    - 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
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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