HomePHP 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.
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:
try{ // instantiate 'SmallDivElementFactory' object (works in the 'small' context) $factoryInstance=new SmallDivElementFactory(); $smalldiv1=$factoryInstance->createWhiteDivElement(); $smalldiv2=$factoryInstance->createBlackDivElement(); // displays small DIVS echo $smalldiv1->display(); echo $smalldiv2->display(); // instantiate 'LargeDivElementFactory' object // (works in the 'large' context) $factoryInstance=new LargeDivElementFactory(); $largediv1=$factoryInstance->createWhiteDivElement(); $largediv2=$factoryInstance->createBlackDivElement(); // display large DIVS echo $largediv1->display(); echo $largediv2->display(); } catch(Exception $e){ echo $e->getMessage(); exit(); }
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!