HomePHP Page 4 - Using Abstract Factory Classes in PHP 5 to Work with Online Forms
Understanding how the abstract factory pattern works - PHP
Any PHP developer who has worked with pattern-based programming in PHP for a while knows that the abstract factory pattern is useful for building classes that return (to client code) objects whose type depend on the content where they're used. Welcome to the final installment of the series "Using abstract factory classes in PHP 5." If you're interested in learning the key concepts of this helpful pattern, this three-part series will teach you how to apply it by developing numerous educational examples.
In order to understand more easily how diverse web form objects can be instantiated in accordance with a predefined context, below I set up an educational example. It shows in a step-by-step format how to display a few "required" and "normal" form elements, in consonance with a given instance of a factory class.
The source code that corresponds to the example in question is as follows:
try{ // instantiate 'RequiredFormElementFactory' object // (works in the 'required' context) $factoryInstance=new RequiredFormElementFactory(); $reqInputBox=$factoryInstance->createInputBox(); $reqRadioButton=$factoryInstance->createRadioButton(); $reqCheckBox=$factoryInstance->createCheckBox(); // displays required form elements echo '<form>'; echo $reqInputBox->display(); echo $reqRadioButton->display(); echo $reqCheckBox->display(); echo '</form>'; // instantiate 'NormalFormElementFactory' object // (works in the 'normal' context) $factoryInstance=new NormalFormElementFactory(); $inputBox=$factoryInstance->createInputBox(); $radioButton=$factoryInstance->createRadioButton(); $checkBox=$factoryInstance->createCheckBox(); // displays normal form elements echo '<form>'; echo $inputBox->display(); echo $radioButton->display(); echo $checkBox->display(); echo '</form>'; } catch(Exception $e){ echo $e->getMessage(); exit(); }
Certainly, you must agree with me that the previous example is indeed demonstrative with reference to showing the functionality of the abstract factory pattern! As you can see, the above script first creates an instance of the "RequiredFormElementFactory" class, and then displays a simple web form, which contains a few "required " form elements. Logically, this is because this concrete factory makes sure that all the instantiated objects belongs to the "required" context.
Next, the example continues doing its thing and creates a new instance of the "NormalFormElementFactory" class, which not surprisingly is tasked with spawning form objects that belong to the "normal" context. Pretty good, right?
As you hopefully learned, in all cases, the concrete factories are completely responsible for making sure that the correct types of objects are returned to client code.
As with all of my articles on web development with PHP, I suggest that you experiment with using and modifying all the classes that I developed here, so you can acquire a better background on how this design pattern works.
Final thoughts
That's all for the moment. In this three-part series, I provided you with a neat set of illustrative examples on how to build and use abstract factory classes with PHP 5.
Although it's more probable that you'll use only singletons and concrete factories during the development of object-oriented applications, there will be situations where you'll need to use abstract factory classes to create objects that must exist only in a predefined context.