Home arrow PHP arrow Page 4 - The Basics of Abstract Factory Classes in PHP 5

Seeing the abstract factory pattern in action - PHP

You have probably used the factory design pattern before. An abstract factory pattern helps you make sure you're creating the correct objects for your application according to the context. This article, the first one in a three-part series, gives you a taste of what you can do with the abstract factory pattern. As always, it includes plenty of examples.

  1. The Basics of Abstract Factory Classes in PHP 5
  2. Introducing the abstract factory pattern: defining an abstract web page element factory
  3. Creating small and large DIV objects
  4. Seeing the abstract factory pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
January 24, 2007

print this article



As I expressed in the previous section, undoubtedly the best way to see how all the previous factory classes fit each other, is by setting up an educational example where they can work together.

The main objective here is to demonstrate how each of the concrete factory classes defined previously can create a correct DIV object in consonance with its respective context. After all, this is the foundation upon which the abstract factory pattern is based.

All right, now that you know how each of the factory classes should work, please pay attention to the following hands on example. It demonstrates in a nutshell, the functionality of the abstract factory pattern. Here is the corresponding code listing, so have a look at it:

   // instantiate 'SmallDivElementFactory' object (works in the 'small' context)
   $factoryInstance=new SmallDivElementFactory();
   // displays small DIVS
   echo $smalldiv1->display();
   echo $smalldiv2->display();
   // instantiate 'LargeDivElementFactory' object
   // (works in the 'large' context)
   $factoryInstance=new LargeDivElementFactory();
   // display large DIVS
   echo $largediv1->display();
   echo $largediv2->display();
catch(Exception $e){
   echo $e->getMessage();

As you can see, the above example is indeed very demonstrative, since it shows how the schema imposed by the abstract factory pattern really works. In this case, you'll realize that each instance of a concrete factory class knows perfectly how to create white and black DIV objects, depending on the context where these factories are used.

Also, with reference to the previous example, once a "SmallDivElementFactory" class has been properly instantiated, two small DIVs are created and displayed on the browser, since this factory instance works in the respective "small" context.

Similarly, when an instance of the "LargeDivElementFactory" class is spawned, the pair of DIVs displayed in this case are logically large elements, since the factory instance works in the "large" context. Quite simple, right?

At this stage, I'm pretty certain that you understand how the abstract factory pattern works, since the previous example is clear enough. However, as with other topics that deal with high levels of abstraction, I suggest that you create your own examples based on the pattern schema that I showed here. I'm sure that the experience will be instructive.

Final thoughts

In this first article of the series, I demonstrated how to develop a few simple factory classes, which came in handy for implementing the abstract factory pattern with PHP 5.

However, we're just beginning to explore this huge subject, since in the next part I'll show you how to apply this useful pattern to work with different instances of AJAX HTTP requester objects. So, if you're interested in learning how this will be achieved, don't miss that tutorial!  

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