This is an attempt at the Factory Method Pattern
It emulates the logical model
and also the physical model
Resources:
http://en.wikipedia.org/wiki/Factory_method_pattern
http://msdn.microsoft.com/en-us/library/ee817667.aspx
http://www.oodesign.com/factory-pattern.html
Is this a correct implementation of the Factory Method Pattern? I think I have followed all the required features of it. Any suggestions are welcome.
/** contract for all flyable vehicles **/
interface iFlyable {
public function fly();
}
/* concrete implementations of iFlyable interface */
class JumboJet implements iFlyable {
public function fly() {
return "Flying 747!";
}
}
class FighterJet implements iFlyable {
public function fly() {
return "Flying an F22!";
}
}
class PrivateJet implements iFlyable {
public function fly() {
return "Flying a Lear Jet!";
}
}
/** contract for conrete Factory **/
/**
* "Define an interface for creating an object, but let the classes that implement the interface
* decide which class to instantiate. The Factory method lets a class defer instantiation to
* subclasses."
**/
interface iFlyableFactory {
public static function create( $flyableVehicle );
}
/** concrete factory **/
class JetFactory implements iFlyableFactory {
/* list of available products that this specific factory makes */
private static $products = array( 'JumboJet', 'FighterJet', 'PrivateJet' );
public static function create( $flyableVehicle ) {
if( in_array( $flyableVehicle, JetFactory::$products ) ) {
return new $flyableVehicle;
} else {
throw new Exception( 'Jet not found' );
}
}
}
$militaryJet = JetFactory::create( 'FighterJet' );
$privateJet = JetFactory::create( 'PrivateJet' );
$commercialJet = JetFactory::create( 'JumboJet' );