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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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