PHP
  Home arrow PHP arrow Page 4 - 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? 
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 - Putting the flyweight factory class to work
    ( Page 4 of 4 )

    As I expressed in the section that you just read, the objective here consists simply of seeing how the previously defined flyweight factory class can be put to work in a useful way. Keeping this in mind, below I created another class, named "WebPageGenerator." It is tasked with displaying dynamic web pages, in this case by using the functionality provided by the flyweight factory.

    Having outlined the behavior for this web page generator class, have a look at its corresponding signature. It is as follows:

    // define 'WebPageGenerator' class
    class WebPageGenerator{
       private $title='Sample Web Page for Flyweight pattern';
       public function addDivElement(DivElement $divElement){
         $this->divElements[]=$divElement;
       }
       public function displayWebPage(){
         $html='<html><head><title>'.$this->title.
    '</title></head><body>';
         foreach($this->divElements as $div){
           $html.=$div->getHTML();
         }
         $html.='</body></html>';
         return $html;
       }
    }

    If you take some time and examine the signature that corresponds to the above class, then you'll quickly grasp how it works. In crude terms, this class accepts objects of type "DivElement," which are used later on to create simple, yet dynamic, web pages.

    Nevertheless, I'm pretty certain that you want to see how the aforementioned flyweight factory class fits into this schema. I developed a simple script (displayed below) which shows how to build a web page that only includes three DIV objects.

    That being said, please analyze the following example:

    // example building a web page using the flyweight design pattern
    try{
       // instantiate 'FlyweightDivElementFactory' object
       $flyweightDivElemFactory=new FlyweightDivElementFactory();
       // instantiate three DIV elements
       $flyweightDivA=$flyweightDivElemFactory->getDivElement
    ('diva');
       $flyweightDivB=$flyweightDivElemFactory->getDivElement
    ('divb');
       $flyweightDivC=$flyweightDivElemFactory->getDivElement
    ('divc');
       // instantiate 'WebPageGenerator' object
    $webPageGenerator=new WebPageGenerator();
       // add DIV objects to web page generator
       $webPageGenerator->addDivElement($flyweightDivA);
      
    $webPageGenerator->addDivElement($flyweightDivB);
       $webPageGenerator->addDivElement($flyweightDivC);
       echo $webPageGenerator->displayWebPage();
    }

    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, creating web documents using both the flyweight factory and web page generator classes is indeed a very clear process, since the code for the above script is easy to follow.

    First, an instance of the flyweight factory class is created to get only the three allowed DIV objects that you learned about in a previous section. Then these DIVs are inputted straight into the web page generator, and finally the pertaining web document is displayed. Quite simple, isn't it?

    However, it's really interesting to see what happens if I try to create a new DIV, other than those identified as "diva," "divb" and "divc". Here is the code that clearly reflects that condition: 

    // try instantiating another DIV element (triggers an exception)
    // $flyweightDivD=$flyweightDivElemFactory->getDivElement
    ('divd');

    In this case, the flyweight factory class really shows its capacity for keeping the number of DIV objects limited to three.

    And finally, here is the corresponding result when I try to instantiate two DIVs that have the same ID attribute:

    // instantiate two identical DIV elements
    $flyweightDivA=$flyweightDivElemFactory->getDivElement('diva');
    $flyweightDivB=$flyweightDivElemFactory->getDivElement('diva');
    if($flyweightDivA===$flyweightDivB){
      throw new Exception('Div element objects are the same!');
    }

    Obviously, two identical objects are returned to calling code, which also triggers an exception.

    At this stage, after you have carefully studied all the examples that I wrote here, you should have a decent background in how to create flyweight classes with PHP 5. As in many other cases, the best approach to take here is one of intensive practice, thus I suggest you to spend some time playing with the all the classes that were shown in this article.

    Final thoughts

    Sad but true, this series has ended. As you learned during this journey, the flyweight design pattern can be useful for controlling the number of class instances created across a given PHP application. In most cases, this approach is followed to improve the performance of a web server that is already overloaded. If this is the situation you are dealing with,  consider using this pattern as part of your PHP applications.

    See you in the next PHP tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT