I’m busy parsing xml documents (google docs api) and putting individual documents into objects.
There are different types of documents (documents, spreadsheets, presentations). Most information about these documents is the same, but some is different.
The idea was to create a base document class which holds all the shared information, while using subclasses for each specific document type.
The problem is creating the right classes for the different types. There are two ways to differentiate the type of the document. Each entry has a category element where I can find the type. Another method that will be used is by the resourceId, in the form of type:id.
The most naive option will be to create an if-statement (or switch-statement) checking the type of the entry, and create the corresponding object for it. But that would require to edit the code if a new type would be added.
Now i’m not really sure if there is another way to solve this, so that’s the reason I’m asking it here. I could encapsulate the creation of the right type of object in a factory method, so the amount of change needed is minimal.
Right now, I have something like this:
public static function factory(SimpleXMLElement $element)
{
$element->registerXPathNamespace("d", "http://www.w3.org/2005/Atom");
$category = $element->xpath("d:category[@scheme='http://schemas.google.com/g/2005#kind']");
if($category[0]['label'] == "spreadsheet")
{
return new Model_Google_Spreadsheet($element);
}
else
{
return new Model_Google_Base($element);
}
}
So my question is, is there another method I’m not seeing to handle this situation?
Edit:
Added example code
Updated answer with your code example
Here is your new factory :
I’m not sure that I got exactly your point… To rephrase the question, I understood “how can I create the right object without hardcoding the selection in my client code”
With autoload
So let’s start with the base client code
Now, let’s see if we can change this to avoid the if / switch statments (not always necessary, but can be)
We’re here gonna use PHP Autoload capabilities.
First, consider the autoload is in place, here is our new Factory
Now we have to add the autoload capability
And you just have to register your autoloader like
Now, everytime you’ll have to write a new handler for Types, it will be automatically added.
With chain of responsability
You may wan’t to write something more “dynamic” in your factory, with a subclass that handles more than one Type.
eg
In the BaseFactory code, you could load all your handlers and do something like