Hack69.Flexible Object Creation with Factory Methods


Hack 69. Flexible Object Creation with Factory Methods

Use the Factory Method pattern when creating objects to allow derived classes to alter what types of objects are created.

Closely related to the Abstract Factory pattern is the Factory Method pattern. This one is fairly commonsense. If you have a class that is creating a lot of objects, you can use protected methods to encapsulate the object creation. That way, a derived class can just override the protected method(s) in the factory to create a different type of object.

In this case, the RecordReader class, instead of using a Factory class, uses a method called newRecord( ) to create a new Record object. That way, a class that derives from RecordReader can change the type of Record objects created by overriding the newRecord( ) method. This is shown graphically in Figure 7-3.

Figure 7-3. The relationship between RecordReader and Record


7.4.1. The Code

Save the code in Example 7-3 as factory_method.php.

Example 7-3. An example of factory methods on a class
 <?php class Record {   public $id = null;   public $first = null;   public $last = null;   public function Record( $id, $first, $last ) {   $this->id = $id;   $this->first = $first;   $this->last = $last; }   } class RecordReader {   function readRecords()   {     $records = array(); $records []= $this->newRecord( 1, "Jack", "Herrington" ); $records []= $this->newRecord( 2, "Lori", "Herrington" ); $records []= $this->newRecord( 3, "Megan", "Herrington" ); return $records;   }   protected function newRecord( $id, $first, $last )   { return new Record( $id, $first, $last );   } } $rr = new RecordReader(); $records = $rr->readRecords(); foreach( $records as $r ) {   print $r->id." - ".$r->first." - ".$r->last."\n"; } ?> 

7.4.2. Running the Hack

You run this hack on the command line using the PHP interpreter:

 % php factory_method.php 1 - Jack - Herrington 2 - Lori - Herrington 3 - Megan - Herrington 

In this code, the RecordReader object is instantiated and its readRecords( ) method is called. This method in turn calls the newRecord( ) method to create all of the Record objects. The created objects are then printed to the console using a foreach loop.

The most visible example of the Factory Method pattern is in the W3C's XML DOM API, installed as part of the PHP 5 base installation. The DOMDocument object, which is at the root of any DOM tree, has a set of factory methods: createElement( ), createAttribute( ), createTextNode(), etc. Any implementation that derived from DOMDocument could override these methods to change what objects were created when XML trees were loaded from disk or strings or created on the fly.

As with the Abstract Factory pattern, the key indicator that you should use the Factory Method pattern is when you are writing an awful lot of code that is doing object creation. Using an Abstract Factory pattern or Factory Method pattern will ensure that if the type of objects you want to create, or how those objects are created, changes, the impact to your code will be minimal.

7.4.3. See Also

  • "Create Objects with Abstract Factories" [Hack #68]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net