Home arrow PHP arrow Page 2 - Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5

Working with AJAX HTTP requester objects - PHP

The abstract factory pattern can be useful if you’re developing a PHP application that needs to create diverse objects in multiple contexts. If you’re interested in learning the basics of this design pattern, this set of articles might be what you’re looking for. Welcome to the second installment of the series “Using abstract factory classes in PHP 5.” In a step-by-step guide, this series teaches you how to create abstract factory classes in PHP 5 by providing you with a wealth of educational examples on the topic.

TABLE OF CONTENTS:
  1. Creating AJAX Requester Objects with Abstract Factory Classes in PHP 5
  2. Working with AJAX HTTP requester objects
  3. Completing the schema imposed by the abstract factory pattern
  4. Demonstrating the functionality of the abstract factory pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
January 31, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To illustrate in a friendly fashion how the abstract factory pattern can be used to spawn different HTTP requester objects, I'm going to define an abstract factory class which will determine what type of AJAX objects will be created by the corresponding concrete factories.

In this case, I decided to provide this abstract factory with the capability to work with text and XML-based HTTP requester objects. Its definition is as follows:

// define abstract 'AbstractAjaxRequesterFactory' class
abstract class AbstractAjaxRequesterFactory{
   abstract public function createAjaxTextRequester();
   abstract public function createAjaxXmlRequester();
}

As you can see, even when the signature for the abstract factory class is brief, it shows clearly the nature of the objects that should be created by two non-abstract factories. In this case, these two concrete factories are responsible for returning to calling code at least two types of requester objects, called "AJAXTextRequester" and "AJAXXmlRequester" respectively.

As you might have guessed, the first requester object will be capable of returning to the client all the server responses as plain text via the corresponding "responseText" property offered by AJAX. The second one will return all the server outputs in XML format, logically, by using the "responseXML" property.

As you'll probably realize, the two types of HTTP requester objects that must be created by the corresponding concrete factories are pretty straightforward. Therefore let me move on and show you the respective signatures for these factories, which will be created as subclasses of the abstract one.

Having said that, take a look at the following child classes, which are responsible for spawning the two kinds of HTTP requester objects mentioned before. Here they are:

 // define concrete 'SynchronousAjaxRequesterFactory' class
class SynchronousAjaxRequesterFactory extends AbstractAjaxRequesterFactory{
   private $context='synchronous';
   public function createAjaxTextRequester(){
     return new SynchronousAjaxTextRequester();
   }
   public function createAjaxXmlRequester(){
     return new SynchronousAjaxXmlRequester();
   }
}
// define concrete 'AsynchronousAjaxRequesterFactory' class
class AsynchronousAjaxRequesterFactory extends
AbstractAjaxRequesterFactory{
   private $context='asynchronous';
   public function createAjaxTextRequester(){
     return new AsynchronousAjaxTextRequester();
   }
   public function createAjaxXmlRequester(){
     return new AsynchronousAjaxXmlRequester();
   }
}

After defining the couple of concrete factory classes listed above, I'm pretty certain that you see more clearly the kind of HTTP requester objects created by each one of them.

With reference to the first factory class, you can see that it works on the "synchronous" context and logically must spawn only synchronous AJAX objects. Meanwhile, the signature of the second factory class demonstrates that only asynchronous objects will be returned to client code. That was pretty simple to grasp, wasn't it?

Besides, you should notice that whether a factory class is used in a "synchronous" or "asynchronous" context, in both cases it's capable of creating text-based or XML-based requester objects.

Well, having explained how each concrete factory class works in its corresponding context, I think it's a good time to move forward and proceed to show the respective definitions for all the classes that are tasked with spawning AJAX objects.

Of course, the signatures that correspond to all these brand new classes will be covered in the next few lines, thus I recommend that you click on the link that appears below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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