Design Patterns in PHP - Factory method

A design pattern is a template used by software engineers as a guideline of how to solve a particular problem.
By Sam Holman
23rd November 2010
A design pattern is a template used by software engineers as a guideline of how to solve a particular problem. The term design pattern was first introduced to the software community in 1994 by the ‘Gang of Four’ or GOF (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) in their book Design patterns, which is still relevant today.

There are many books and articles with example implementations of design patterns in all sorts of languages, but very few in the PHP world.
Whilst by their very definition a design pattern is language agnostic I feel it can be of benefit, especially to single language programmers, to see example implementations in their native language.

In this series of articles I plan to explore some of the design patterns that i have found most useful in my time as a PHP developer, providing examples and notes. So, this being the first, we’ll take a look at the Factory Method Pattern.
The factory method pattern provides an interface to get a class without requiring the caller to know exactly what type of class it requires. This is done by providing a Factory method, often called ‘Factory’, which is then responsible for creating objects of this type or some derived class.
Below is an example of how to implement a factory pattern which selects the correct class to handle an image type. It’s worth bearing in mind that the file extension isn’t the most reliable way to determine image type, but it works as an example. :-)
Example:
Image.inc.php
_fileName = $fileName;
        }
        else {
            throw Exception('Invalid File name');
        }
    }

    abstract public function readImage();

    public static function Factory($fileName)
    {
        $extension = substr($fileName, strrpos($fileName, '.') + 1);

        switch ($extension)
        {
            case ‘jpg’:
            case ‘jpeg’:
                return new Image_JPG($fileName);
                break;
            case ‘png’:
                return new Image_PNG($fileName);
                break;
            default:
                throw new Exception('Unknown image type');
        }
    }
}
Image/JPG.inc.php
Image/PNG.inc.php
Some key things to note in this example are:
The return type of the factory method
When using the Factory in the example above you can guarantee that you will get something that extends Image (or be thrown an exception) and therefor it is safe to call the readImage method on the result.
The use of a static method as the factory.
The factory method pattern describes a method that is used to create objects, this method can be located as a static method of the super class. If implemented in this way the main advantage is the ability to protect the constructor.
The use of a private __construct method.
This is not directly part of the design pattern but I usually find it to be a useful addition as it prevents the objects from being created by anything other than the factory method.

Comments

Rommel 19th June 2012
thank you, i'm looking for examples of Factory pattern with PHP
Petar 14th September 2012
It's a little tricky for me to distinct the strategy pattern from the factory :( Both cases we don't know which class we will initiate, both cases we want a concrete task to be done without interest in the methods used.

Add your own comment

© 2013 Label Media