PHP
  Home arrow PHP arrow Page 3 - Generating Web Pages with the Flyweight Pattern in 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

Generating Web Pages with the Flyweight Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-03-05


    Table of Contents:
  • Generating Web Pages with the Flyweight Pattern in PHP 5
  • Building dynamic web page elements
  • Defining a flyweight factory class
  • Putting the flyweight factory class to work

  • 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


    Generating Web Pages with the Flyweight Pattern in PHP 5 - Defining a flyweight factory class
    ( Page 3 of 4 )

    To develop a web page generator system capable of maintaining balance with the number of DIV objects created across the application, I must first define a flyweight class. This class will be provided with the capacity to return only three DIVs to client code, identified as "diva","divb", and "divc" respectively.

    Naturally, in accordance with the logic that stands behind the flyweight pattern, this factory class will refuse any attempts to create more DIV objects, in this manner keeping in equilibrium the number of objects used by the web page generator application.

    Now that you know how this flyweight class is going to work, I'd like you to pay attention to its signature, which is listed below:

    // define 'FlyweightDivElementFactory' class
    class FlyweightDivElementFactory{
       private $divElements=array();
       public function __construct(){
         $this->divElements['diva']=NULL;
         $this->divElements['divb']=NULL;
         $this->divElements['divc']=NULL;
       }
       // return to calling code only three DIVS
       public function getDivElement($divId){
         if($divId!='diva'&&$divId!='divb'&&$divId!='divc'){
           throw new Exception('Invalid ID attribute for DIV
    element!');
         }
         if($this->divElements[$divId]==NULL){
           $this->divElements[$divId]=new DivElement($divId);
         }
         return $this->divElements[$divId];
       }
    }

    You can see that the above flyweight factory class has one relevant method, called "fetchDivElement()," which is responsible for controlling the number of DIV objects that will be returned to client code. As one would expect, if an invalid $divID argument is passed to this method, an exception will be triggered, and program's execution will be stopped.

    At this stage, hopefully you'll have a much better idea of how the previous flyweight factory class does its business. So, provided that my assumption here is correct, it's time to move forward and see how this class can be put to work to generate dynamic web pages that include only three DIV elements.

    The experience sounds interesting, therefore if you wish to learn how the previous flyweight class will be used to create web documents on the fly, please jump into the following section. Don't worry, since 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek