1
\$\begingroup\$

This is an attempt at the Factory Method Pattern It emulates the logical model Client--Factory--Product

and also the physical model

enter image description here

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' );
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
$militaryJet = JetFactory::create( 'FighterJet' );
$privateJet = JetFactory::create( 'PrivateJet' );
$commercialJet = JetFactory::create( 'JumboJet' );

This is a Factory pattern. I'm not sure that I'd call it the Factory method pattern. Look at the Wikipedia example for C# -- this looks like the before version. If you were doing the same thing as the after version here, you'd have

$militaryJet = FighterJetFactory::create();
$privateJet = PrivateJetFactory::create();
$commercialJet = JumboJetFactory::create();

Where FighterJetFactory, PrivateJetFactory, and JumboJetFactory would all extend or implement JetFactory.

The question here is why not just say

$militaryJet = new FighterJet();
$privateJet = new PrivateJet();
$commercialJet = new JumboJet();

Given this example, you don't need any variation of the Factory pattern. You could just create the objects directly. As a general rule, you shouldn't look to use design patterns. You should meet problems that can be solved by the design pattern. You don't have a problem that needs a design pattern to solve.

Example of a problem that needs a Factory pattern solution: given a passenger count and a cargo weight, return an object representing a plane that is capable of carrying those passengers with that cargo.

Example of a problem that needs a factory method solution: define an interface for a plane and another that will allow you to create a plane object. Give the interfaces to two partners. Have one write a BoeingFactory that implements your factory interface and the other an AirbusFactory. Both should return plane objects that implement your plane interface.

\$\endgroup\$
1
  • \$\begingroup\$ Well, like they say, "There are several implementations of it." With the iFylable interface you can add other concrete products like: Rocket, Helicopter. No worries. It was just an exercise for a textbook. \$\endgroup\$
    – Robert
    Jan 8, 2015 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.