PHP
  Home arrow PHP arrow Page 3 - Generating Web Pages with the Flyweigh...
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

Generating Web Pages with the Flyweight Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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


       · In this final article of the series, the flyweight pattern is used to develop a...
       · Sorry, but having read the first part "Introducing the Flyweight Pattern with PHP...
       · Hello Gregor,Thank you for commenting on my PHP article. Now, with reference to...
       · this article is not about the flyweight pattern at all, consider reading about them...
       · Thank you for commenting on my PHP article about implementing the flyweight pattern...
     

       

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