Factory Design Pattern Example

Factory business logic

Factory Design Pattern are examples of creational patterns which abstract the object instantiation process. They hide how the objects are created and help make the overall system independent of how its object are created and composed.

When we use Factory Design Pattern

The Factory pattern is a good solution for the cases that the main part of the app needs to manage the objects and not create them.

  • If a class manages a shopping cart, it should deal only with shopping cart management (e.g., adding or removing products from the cart) and not be concerned with the manufacturing of those products.
  • A warehouse management class follows the quantities of items entering and leaving the warehouse but has no interest in making those items
  • A car buying agency should simply track orders and not have to also deal with the details of how the cars are made.

An example of Factory design pattern

We will apply factory pattern to shopping business. Let’s see the overview diagram of our factory pattern
Factory design pattern

Step 1: Create an interface

Product.interface.php

interface Product {
    public function getPrice();
    public function getDescription();
    public function getImage();
}

Step 2: Create concrete classes implementing the same interface

Keyboard.class.php

class Keyboard implements Product {
    public function getPrice() {
        return 50;
    }
    public function getDescription(){
        return 'Keyboard description';
    }
    public function getImage() {
        return null;
    }
}

Mouse.class.php

class Mouse implements Product {
    public function getPrice() {
        return 150;
    }
    public function getDescription(){
        return 'Mouse description';
    }
    public function getImage() {
        return null;
    }
}

Monitor.class.php

class Monitor implements Product {
    public function getPrice() {
        return 250;
    }
    public function getDescription(){
        return 'Monitor description';
    }
    public function getImage() {
       return null;
    }
}

Step 3: Create a Factory to generate object of concrete class based on given information.

ProductFactory.class.php

class ProductFactory {
    /**
    * Define the logic to create new object
    */
    private function make($productId) {
       $preFix = substr($productId, 0, 3);

        switch($productId) {
            case 'key':
                return new Keyboard();
                break;
            case 'mou':
                return new Mouse();
                break;
            case 'mon':
                return new Monitor();
                break;
        }
    }
}

Step 4: Use the Factory to add product to shopping cart

class ShoppingCart {
    private $productsInTheCart = [];
    private $productFactory;

    public function __construct() {
        $this->productFactory = new ProductFactory();
    }

    function add($productId) {
        // add product to cart, don't care about the way we create the object
        $this->productsInTheCart[] = $this->productFactory->make($productId);
    }
}

Conclusion

This is a simple example of Factory Design Pattern, We hope that you will get the main idea of Factory Pattern then we can apply it appropriately.

Let’s checkout other post in Design Patterns series

Related Post

Leave a Reply