Home arrow PHP arrow Page 4 - Generating Web Pages with the Flyweight Pattern in PHP 5

Putting the flyweight factory class to work - PHP

Unnecessary and balanced instantiation of PHP classes are issues that can be easily solved by using the flyweight design pattern. If you want to learn more about it, you should start reading this article. Welcome to the final part of the series “Using the flyweight pattern with PHP 5.” As you may have guessed, this series walks you through the implementation of this helpful pattern with PHP, and shows you how to apply it in concrete cases.

TABLE OF CONTENTS:
  1. Generating Web Pages with the Flyweight Pattern in PHP 5
  2. Building dynamic web page elements
  3. Defining a flyweight factory class
  4. Putting the flyweight factory class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
March 05, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: