PHP
  Home arrow PHP arrow Page 2 - 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 - Building dynamic web page elements
    ( Page 2 of 4 )

    Since in the introduction that you just read I stated that I was going to demonstrate how to use the flyweight pattern for building dynamic web pages, my first step is to define a basic class, called "DivElement," which will be tasked with displaying DIVs on a web document.

    Once you have grasped the logic followed by this class, I'll create a flyweight class which will be responsible for keeping the number of DIV objects limited to a specific number.

    But for the moment, pay attention to the signature of the following "DivElement" class, which looks like this:

    // define 'DivElement' class
    class DivElement{
       private $id;
       private $class;
       private $content;
       public function __construct($id='defaultid',
    $class='defaultclass',$content='Default content for DIV goes
    here.'){
         if(!preg_match("/[a-zA-Z]+/",$id)){
           throw new Exception('Invalid value for ID property!');
         }
         if(!preg_match("/[a-zA-Z]+/",$class)){
           throw new Exception('Invalid value for name property!');
         }
         if(!$content){
           throw new Exception('Invalid content for div element!');
         }
         $this->id=$id;
         $this->class=$class;
         $this->content=$content;
       }
       // fetch (X)HTML markup of div element
       public function getHTML(){
         return '<div id="'.$this->id.'" class="'.$this-
    >class.'">'.$this->content.'</div>';
       }
    }

    As you'll realize, the above "DivElement" class is quite simple to follow. In consonance with the concepts that I deployed a few lines above, it's tasked with displaying a generic containing DIV on a web document. So far, this isn't rocket science, right?

    Logically, a possible implementation for the previous class, to build a simple web page, could be coded as follows:

    // example building a web page using the 'DivElement' class
    try{
       // instantiate DivElement objects
       $divA=new DivElement('diva','clsa','Content for DIV A goes
    here.');
       $divB=new DivElement('divb','clsb','Content for DIV B goes
    here.');
       $divC=new DivElement('divc','clsc','Content for DIV C goes
    here.');
       // display web page
       echo '<html><head><title>Sample Web Page</title></head><body>'.$divA->getHTML().$divB->getHTML
    ().$divC->getHTML().'</body></html>';
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the prior script shows how to use the "DivElement" class that I defined previously to create a sample web document that contains only three DIVs. The example is very easy to grasp, so you shouldn't have any problems understanding how it works.

    However, the aforementioned "DivElement" class apparently has nothing to do with applying the flyweight pattern. Well, not so fast. Suppose that you want to develop an application that generates dynamic web pages with a few DIV objects, but at the same time, you need to keep the amount of instantiated DIVs limited to a specific number. How can this be done?

    Yes, you guessed right! The answer to the above question is obviously using the flyweight pattern. Doing so, it's feasible to create a web page generator system that not only constructs web documents on the fly, but keeps the number of DIV objects required to build the web pages in question completely under our control.

    Therefore, taking into account that the flyweight pattern now starts to play a relevant role, in the course of the section to come, I'm going to show you how to create a flyweight class which will be capable of controlling (and balancing, by the way), the number of instances that correspond to the aforementioned "DivElement" class.

    Want to find out how this brand new class will be defined? Visit the next section and keep reading.



     
     
    >>> 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