PHP
  Home arrow PHP arrow Page 3 - 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 - 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


       · 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 6 hosted by Hostway
    Stay green...Green IT