• RSS
  • Print this article!
  • Digg
  • del.icio.us
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • Design Float
  • Reddit
  • StumbleUpon
  • Technorati
  • Live
  • TwitThis
Home > Development Best Practices > Factory Method Design Pattern in PHP

Factory Method Design Pattern in PHP

June 1st, 2009

What is the Factory Method Design Pattern? and why should I use it? First off we should look at what a design pattern is. If you’re not sure you can visit: Design Patterns: Soliving web development problems and Singleton Design Pattern: When one instance is enough. It might help you get a grasp on design patterns.


factory-method-uml

What is the Factory Method Design Pattern?

You are creating an interface for creating objects, but let the subclasses decide which class to use. This is just like a real life factory. It creates the type of product you want based on the order you put in. What ever is inside that factory decides on which tools to use to get the job done.

Say we have a factory that makes all kinds of cars. For this instance we’ll be creating a Honda, Toyota, or a Mazda. If we place an order saying we want a Honda, then we expect a Honda is going to come out of that factory. We don’t need to know how it’s made, or how it’s different from other cars, just that we get what we asked for. That’s the beauty of the Factory Method design pattern. It encapsulates how things are made, so you don’t need to know all the details. This is huge, because if a car changes the way it was made, you as the developer don’t have to worry about it. The car’s class will change, but how you access that class remains unchanged. This is abstraction at it’s best. Here is an example in PHP

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

//Generic car information. Information that applies to all cars.
abstract class Car {
   
    public function getType(){
        echo $this->type;
    }
}

//Class that holds all the details on how to make a Honda.
class Honda extends Car{
    private $type = "Honda";
   
    public function getType(){
        echo $this->type;
    }
    //Add any code that makes this car different.
}

//Class that holds all the details on how to make a Toyota.
class Toyota extends Car{
    private $type = "Toyota";
   
    public function getType(){
        echo $this->type;
    }
    //Add any code that makes this car different.
}

//Class that holds all the details on how to make a Mazda.
class Mazda extends Car{
    private $type = "Mazda";
   
    public function getType(){
        echo $this->type;
    }
    //Add any code that makes this car different.
}

//The Car factory.  Input the type of car you want, and it will give it to you.
class CarFactory {
   
    //Different types of cars.
    const HONDA = "Honda";
    const TOYOTA = "Toyota";
    const MAZDA = "Mazda";
   
    //Make the car that is selected.
    public static function createCar($carType){
        switch($carType){
            case self::HONDA:
                return new Honda();
                break;
            case self::TOYOTA:
                return new Toyota();
                break;
            case self::MAZDA:
                return new Mazda();
        }
        die("Car isn't recognized.");
    }
   
}

//Make a car factory
$carFactory = new CarFactory();
//Create a Car
$Honda = $carFactory->createCar(CarFactory::HONDA);
//Run a method.
$Honda->getType();

$Mazda = $carFactory->createCar(CarFactory::MAZDA);
$Mazda->getType();

$Toyota = $carFactory->createCar(CarFactory::TOYOTA);
$Toyota->getType();

?>

Why Should I use the Factory Method Design Pattern?

With developers time being mostly spent on updating or fixing previous code, using the factory method design pattern will help expedite the process. If you know that an item has to change, you can go straight to the item’s own class and change it all you like. Its encapsulated so you don’t have to worry about changing how that class is accessed. Also if you ever have to add a new item you know where to go…straight to the factory class.

Hopefully this was helpful and will get you started on using this very useful design pattern.

  • RSS
  • Print this article!
  • Digg
  • del.icio.us
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • Design Float
  • Reddit
  • StumbleUpon
  • Technorati
  • Live
  • TwitThis
Comments are closed.