Home arrow PHP arrow Introducing the Flyweight Pattern with PHP 5

Introducing the Flyweight Pattern with PHP 5

Among the considerable variety of structural design patterns that can be implemented with PHP 4 (and PHP 5, by the way), thereís one in particular that deserves special attention. It's easy to apply in the context of a given web application, and it offers remarkable functionality when it comes to preventing the unnecessary instantiation of different classes. This two-part series covers that pattern.

  1. Introducing the Flyweight Pattern with PHP 5
  2. Defining a target class
  3. Defining a flyweight factory class
  4. Seeing the flyweight pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 19
February 26, 2007

print this article



Naturally, if you already read the title of this series, then youíll know that Iím speaking specifically of the flyweight pattern. But before I go into further explanations, let me tell you briefly how this pattern works. This will give you a better idea of how and when it can be used.

In simple terms, when the flyweight pattern is applied, itís possible to specify programmatically that a particular class is going to have only a predefined number of instances, which are shared within the same implementation. This simple concept itself introduces an immediate benefit: it prevents unnecessary instantiations of the class in question across the same application, which results in a noticeable improvement in the performance of certain systems that must work with heavy loads.

Certainly, if you reread the above definition, itís clear to see that a flyweight class can be considered a straightforward approach to keep the creation of objects carefully balanced. This can improve the web serverís overall performance, as I mentioned a few lines above.

Nonetheless, this series isnít limited to introducing boring theory on how the flyweight pattern works. You'll have the chance to learn and test many practical examples related to the referenced pattern, so you can quickly start applying it in your own PHP applications.

A final note before I proceed further: all the examples that youíll see here will be developed with PHP 5, but they can be easily modified to work with PHP4 as well.

Having clarified that point, letís find out together how to implement flyweight classes with PHP. Itís going to be instructive and fun!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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