Recursion as Client
Previous posts on this blog discussed Recursion and the Factory Method design pattern. In working with a recursive object to traverse a list of strings or numbers (or any other objects for that matter), PHP developers typically target an array. In developing a simple recursive object, I realized that it made more sense to set up an another class for the array object. Why bind the process or creating and populating an array to the object designed to transverse it? After all, what if you wanted to use the object to traverse any list; not just the one bound to the recursion operation?
The Factory Method for Handling Array Objects
From the not-too-difficult decision to separate the array creation and population process from the transversal process, it was a pretty quick leap to using the Factory Method. That would give me plenty of flexibility, and I would be able to use the factory implementations to do different things with the products—like sort or filter them before returning them to the requesting client.
In this case, the requesting client would be the Recursion object.
The Client class triggers the appropriate method in the Recursion object to select whatever list is available. The selection process would be handled by an HTML UI. The class diagram is integrated into the application. Click the Play button below first to see the operation and class diagram and then the Download button to get all of the files:
When you Play the program, notice in the class diagram that the Recursion class includes a method, getArray(Factory). This method is an example of programming to the interface and not the implementation. The method expects an argument that is a Factory child, but it can be any Factory implementation. The type hint is the Factory interface and so any implementation of Factory works fine. As a result, you can add as many different Factory implementations as you want without having to worry about a train wreck like you can get easily by programming to implementations.
The Recursion Class Makes use of the Factory
As noted at the outset, the Recursion object is set up to use any Factory implementation. In this example, only three implementations use the Factory interface. The factory method itself is makeArray(), but it’s left to the implementations how the array to be returned to the requester—the Recursion class. The PeopleFactory and PetsFactory sort the list in alphabetical order before returning it to the requesting client. However, the ToolsFactory uses a reverse sort so that the highest Wrench element is first and the Awl is last. The recursive iterator in the Recursion object could care less; it just transverses whatever list it gets from the Factory implementation and prints it to the screen.
class Recursion
{
//The Recursion class is the Client
//in making a request from the Factory
private $members;
private $counter=0;
private $size;
public function getPeople()
{
//Makes request to Factory child
$factoryNow=new PeopleFactory();
$this->getArray($factoryNow);
}
public function getTools()
{
//Makes request to Factory child
$factoryNow=new ToolFactory();
$this->getArray($factoryNow);
}
public function getPets()
{
//Makes request to Factory child
$factoryNow=new PetsFactory();
$this->getArray($factoryNow);
}
//By programming to the interface (Factory)
//you can use different implementations
private function getArray(Factory $factoryBuild)
{
$this->members=$factoryBuild->makeArray();
$this->size=count($this->members)-1;
$this->recursive();
}
private function recursive()
{
if($this->counter <= $this->size)
{
echo "" . $this->members[$this->counter] . "
|
As you can see, the recursive() method is pretty simple. It just keeps using echo to send the current element value to the screen, increments the counter by 1, and then if it hasn’t exceeded the length of the array, it calls itself to repeat the process. The Factory implementations already took care of the sorting arrangements–or anything else developers want the Factory implementations to do.
Each of the 3 public “getter” methods call the private getArray(Factory) method using a Factory implementation as an argument. The passed argument calls the factory method (makeArray()) to get the requested array which is stored in the $members property. Finally, getArray(Factory) calls the recursive() iteration method that traverses the $members property containing the array.
A Lot of Work for Iteration
If you’re thinking
…that’s a lot of work for a simple operation (displaying sorted lists on the screen)
I’d not disagree. The point of design patterns, though, is re-use on a much larger scale. As projects get bigger and bigger, you need tools to deal with them. The good news is that if you can use them on a small scale like the examples on this blog and in Learning PHP Design Patterns, you’re prepared to work on larger projects involving teams of programmers and some seriously good applications.
Copyright © 2014 William Sanders. All Rights Reserved.
0 Responses to “PHP Recursion Using Factory Method: Programming to the Interface”