HomePHP Page 3 - The Basics of Using the Factory Pattern in PHP 5
Defining some array processing classes - PHP
If you need to create multiple objects that belong to the same family, you probably want to use the factory pattern. This three-part series takes a close look at using the factory pattern in PHP.
As you'll certainly recall from the prior section, the two concrete factory classes defined in that part of the article were responsible for creating distinct types of array-processing objects, where each of them is backed up by an originating class.
Therefore, in the new few lines I'm going to list the complete definitions for all of these array handling classes, so you can have a better idea of how they look and work.
Here are the signatures for these classes:
// define abstract 'ArrayObj' class abstract class ArrayObj{ abstract public function getArraySize(); abstract public function getArrayElements(); }
// define concrete 'UppercasedNumericArrayObj' class class UppercasedNumericArrayObj extends ArrayObj{ private $inputArray=array('Element 1','Element 2','Element 3'); public function getArraySize(){ return count($this->inputArray); } public function getArrayElements(){ return array_map(strtoupper,$this->inputArray); } }
// define concrete 'LowercasedNumericArrayObj' class class LowercasedNumericArrayObj extends ArrayObj{ private $inputArray=array('ELEMENT 1','ELEMENT 2','ELEMENT 3'); public function getArraySize(){ return count($this->inputArray); } public function getArrayElements(){ return array_map(strtolower,$this->inputArray); } }
// define concrete 'UppercasedAssociativecArrayObj' class class UppercasedAssociativeArrayObj extends ArrayObj{ private $inputArray=array('Element 1'=>'This is element 1','Element 2'=>'This is element 2','Element 3'=>'This is element 3'); public function getArraySize(){ return count($this->inputArray); } public function getArrayElements(){ return array_map(strtoupper,$this->inputArray); } }
// define concrete 'LowercasedAssociativecArrayObj' class class LowercasedAssociativeArrayObj extends ArrayObj{ private $inputArray=array('ELEMENT 1'=>'THIS IS ELEMENT 1','ELEMENT 2'=>'THIS IS ELEMENT 2','ELEMENT 3'=>'THIS IS ELEMENT 3'); public function getArraySize(){ return count($this->inputArray); } public function getArrayElements(){ return array_map(strtolower,$this->inputArray); } }
As you can see, the group of array processing array classes listed above presents two methods, named "getArraySize()" and "getArrayElements()" respectively, which are common to all of them. However, regardless of the simple logic implemented by these classes, you should notice that the first pair is defined to work in a "numeric" context, while the other two are conceived for use in a "associative" environment.
Here, it's clear to see how the factory pattern works in a nutshell, since the two factory classes defined in the previous section must return only array processing objects that conform a set of predefined rules to calling code. Now are you starting to grasp the logic that stands behinds the factory pattern? I bet you are!
All right, having analyzed in detail how all the array handling classes previously created work, it's a good time to develop an example in which all these classes will be put to work in tandem. As you may have guessed, the reason to code this practical example is simply to demonstrate the functionality provided by the pair of factory classes that you learned before.
Want to see how this example will be set up? Read the following section. I'll be there, waiting for you.