DEV Community 👩‍💻👨‍💻

Osaigbovo Emmanuel
Osaigbovo Emmanuel

Posted on • Updated on

Factory Design Pattern (simple example implementation in PHP)

NOTE - Please everything written down here is extracted from a book which I read years ago and actually jotted down in a notebook (to be honest I don't remember the name of this book), I realized that keeping this information inside a notebook is not the best of options, come on what if something happened to my notebook? Also, I wanted this to be available to everyone who might have been wanting to understand the Factory Design Pattern as well -:)

If you have not read my previous post on Strategy Design Pattern Pattern, please do well to check it out.

The Factory design pattern becomes useful in situations where the type of object that needs to be generated isn't known when the program is written but only once the program is running.

Another clue for when the factory pattern might be appropriate is when there is an abstract base class and different subclasses will need to be created on the fly.

This particular design structure is important with the factory pattern, as once you have created the object, regardless of its specific type, the use of the object will be consistent.

The factory design pattern works via a state method,commonly named create(), factory(), factoryMethod() or createInstance(). The method takes at least one argument which indicates the type of object to create, the method returns an object of that type.


 + create ($type) : Product

Concrete Factory          ========>  Object of that class 
                                     type eg Product
+ create ($type) : Product 
Enter fullscreen mode Exit fullscreen mode

In the factory design pattern, the abstract factory base class is extended by concrete factory classes that will output objects of that class type.

Let's see how this will be implemented in the following example. which will create a different type of shape based on the input provided to the page through the URL.

To create a Factory

1) Add a new PHP Script in your text editor or IDE to be named ShapeFactory.php

2) Start defining the ShapeFactory class:

abstract class ShapeFactory {
 // the factory class will be abstract, just as the shape itself is. you will never create an instance of the ShapeFactory class. 
Enter fullscreen mode Exit fullscreen mode

3) Start defining the static method

public static function create($type, array $sizes) {
// this static method will take two arguments: the type of shape to create and an array of sizes. For a rectangle, that array would contain two values, for a triangle, three, and for a circle, only one (the radius)
Enter fullscreen mode Exit fullscreen mode

4) create a different object based on that type of value

switch($type) {
case 'rectangle'
 return new Rectangle($sizes[0], $sizes[1]);
case 'triangle'
 return new Triangle($sizes[0], $sizes[1], $sizes[3]);
case 'circle'
 return new Circle($sizes[0]);
}//end of switch
Enter fullscreen mode Exit fullscreen mode

The switch checks the value of $type against the expected value for now just these shapes are recognized.

NOTE - A complete version of this class would throw an exception if improper values were provided

To use the ShapeFactory class

$obj = ShapeFactory::create($GET['shape'], $GET['dimentions']);

to create the object, the static method of the ShapeFactory class is invoked, passing it the value that came from the URL.

In a more realized version of this script, one would add error handling here to confirm that the shape object was created before attempting to use it in the following steps
$area = $obj->getArea();

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.