Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5 - Demonstrating the functionality of the abstract factory pattern
(Page 4 of 4 )
The best way to demonstrate the functionality offered by the abstract factory pattern is simply by setting up an instructive example where all the previously defined classes are used in conjunction.
Bearing in mind the strong educational sense of the example in question, below I coded a short script. It illustrates how the two concrete factories that you learned in a previous section are capable of returning the correct type of AJAX objects to client code, depending on the context where they're used.
Here is the sample script:
try{
// instantiate 'SynchronousAjaxRequesterFactory' object
// (works in the 'synchronous' context)
$factoryInstance=new SynchronousAjaxRequesterFactory();
$syncRec1=$factoryInstance->createAjaxTextRequester();
$syncReq1->send();
$syncReq2=$factoryInstance->createAjaxXmlRequester();
$syncReq2->send();
// instantiate 'AsynchronousAjaxRequesterFactory' object
// (works in the 'asynchronous' context)
$factoryInstance=new AsynchronousAjaxRequesterFactory();
$asyncReq1=$factoryInstance->createAjaxTextRequester();
$asyncReq1->send();
$asyncReq2=$factoryInstance->createAjaxXmlRequester();
$asyncReq2->send();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As illustrated above, after the first synchronous factory class has been instantiated, it makes sure that only synchronous AJAX objects are returned to client code, following the logic implemented by the abstract factory pattern. Similarly, once a new instance of the asynchronous factory class is created, naturally only asynchronous AJAX objects will be spawned. Simple and efficient!
At this stage, I believe that the example shown above should give you an accurate idea of how the abstract factory pattern works. So go ahead with this recently-acquired background and start building your own abstract factory classes!
Final thoughts
Sadly, this is the end of this article. However, the overall experience has been educational, since you learned how to use the abstract factory pattern in PHP 5, in order to create AJAX objects that depend on a predefined context.
But do you think this journey is over? Not at all, because in the last installment of this series, I'm going to show you how to use this useful pattern to build programmatically online forms. You won't want to miss it.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |