PHP
  Home arrow PHP arrow Page 3 - 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 - Defining a flyweight factory class
    ( Page 3 of 4 )

    In accordance with the concepts that I expressed in the previous section, applying the flyweight pattern here is only a matter of defining a factory class that will be capable of returning to client code only three instances of the “InputTextBox” class that was previously defined.

    In fact, this sounds like a simple thing to do, but this theory must be translated into a fully-functional class. Thus, in response to this requirement, below I defined a flyweight factory class. It obviously returns three text input objects to calling code. The signature for this brand new class is as follows:

    // define 'FlyweightFormElementFactory' class
    class FlyweightFormElementFactory{
       private $formElements=array();
       public function __construct(){
         $this->formElements['name']=NULL;
         $this->formElements['address']=NULL;
         $this->formElements['email']=NULL;
       }
       // return only three text input boxes
       public function fetchFormElement($elementName){
         if($elementName!='name'&&$elementName!=
            'address'&&$elementName!='email'){
           throw new Exception('Invalid name for form element!');
         }
         if($this->formElements[$elementName]==NULL){
           $this->formElements[$elementName]=new TextInputBox
    ($elementName);
         }
         return $this->formElements[$elementName];
       }
    }

    As you can see, the above flyweight factory class exposes only one method (aside from the constructor), called “fetchFormElement(),” which is responsible for creating three instances of the respective “TextInputBox” class created previously.

    But what’s the point of doing this? Well, if you use the above factory class to build a simple contact form, it always returns the same input text objects to calling code, no matter how many times the class is invoked across the same application. Logically, this prevents the unnecessary instantiation of objects that aren’t really required, in this way improving the overall performance of any application. Pretty good, isn’t it?

    However, once you understand properly how the previous flyweight factory class works, there another step to make in order to demonstrate the functionality of the class in question.

    Therefore, in the following section I’m going to set up a practical example that will show you how to use the previous flyweight factory class to create a basic online form that only contains three input boxes.

    Want to see how this will be achieved? Okay, go ahead and read the next few lines. I’ll be there, waiting for you.



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