HomePHP Page 2 - The Basics of Abstract Factory Classes in PHP 5
Introducing the abstract factory pattern: defining an abstract web page element factory - 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.
In consonance with the concepts that you just read in the introduction, first I'm going to define an abstract factory class aimed at creating generic web page elements. As you'll see shortly, this class will define what web page objects will be created by two concrete factories, in this way implementing the schema required by the factory pattern.
Having explained the purpose of defining this abstract factory class for creating web page objects, here is its signature:
// define abstract 'WebPageElementFactory' class abstract class AbstractWebPageElementFactory{ abstract public function createWhiteDivElement(); abstract public function createBlackDivElement(); }
As you can see, the abstract factory class defines clearly what type of objects must be created by two additional concrete factories. In this case, these two non-abstract classes will be tasked with spawning two specific kind of objects: white and black DIV elements respectively.
However, the signature for the above abstract class does not say much about how to implement the abstract factory pattern. Therefore. let me go one step further and derive two subclasses from the abstract one. these will offer a concrete implementation for creating different types of DIV elements.
Having said that, the respective definitions for these two new subclasses are as follows:
// define concrete 'SmallDivElementFactory' class class SmallDivElementFactory extends AbstractWebPageElementFactory{ private $content='small'; public function createWhiteDivElement(){ return new SmallWhiteDivElement; } public function createBlackDivElement(){ return new SmallBlackDivElement; } } // define concrete 'LargeDivElementFactory' class class LargeDivElementFactory extends AbstractWebPageElementFactory{ private $context='large'; public function createWhiteDivElement(){ return new LargeWhiteDivElement; } public function createBlackDivElement(){ return new LargeBlackDivElement; } }
You'll definitely agree with me that things are getting really interesting! Please, notice how the two previous concrete factory classes first define the respective contexts where they're going to work, that is "small" and "large," and then return to client code the correct DIV objects.
In the first case, the factory class insures that either a black or white DIV object will be always created in the "small" context, while in the second case, objects will be spawned in the "large" environment. Now, are you starting to grasp the logic behind the abstract factory pattern? I hope you are!
Okay, at this point I have shown you how the abstract factory looks, as well as how the respective concrete factories were defined. Therefore I think it's time to move forward and proceed to create the group of classes that are tasked with generating black and white DIV objects according to the respective contexts.
Do you want to see how these factory classes will be defined? All right, jump into the following section and keep reading. I'll be there, waiting for you.