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 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 catch(Exception $e){ 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) 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 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|