php factory model

Keywords: PHP less Programming

The Concept of Factory Model

The factory pattern is a class that has some methods for creating objects for you. You can create objects using factory classes instead of using new directly. In this way, if you want to change the type of object you create, you just need to change the factory. All code that uses the factory changes automatically.

Different factory models in 3

1. Simple Factory Mode (Static Factory Mode): Creating Objects by Static Method
2. Factory pattern: Define an object interface that allows subclasses to determine specific object instantiation
3. The abstract factory pattern: Using object composition, the creation of objects is exposed in the factory interface.

Code instance

/**
 * Simple Factory Pattern: Implementing Object Creation by Static Method
 * Interface Drink
 */
interface Drink{
    public function buy();
}

class Coffee implements Drink {

    public function buy(){
        echo "i want to coffee";
    }

}

class Cola implements Drink{

    public function buy()
    {
        echo "i want to cola";
    }

}

class DrinkStore{

    public static function orderCoffee(){
        return new Coffee();
    }

    public static function  orderCola(){
         return new Cola();
    }
}

$man = DrinkStore::orderCoffee();
$man->buy();//Result output i want to coffee
echo "<br/>";
$woman = DrinkStore::orderCola();
$woman->buy();//Result output i want to cola
/**
 * Factory pattern: Object creation is achieved by defining an object's interface and subclasses
 * Interface Drink
 */
interface Drink{
    public function buy();
}

class Coffee implements Drink {

    public function buy(){
        echo "i want to coffee";
    }

}

class Cola implements Drink{

    public function buy()
    {
        echo "i want to cola";
    }

}

interface CreateDrink{
    public function create();
}

class CoffeeFactory implements CreateDrink{
    public function create()
    {
        return new Coffee();
    }
}

class ColaFactory implements CreateDrink{
    public function create()
    {
        return new Cola();
    }
}


$coffee_factory = new CoffeeFactory();
$man = $coffee_factory->create();
$man->buy();//Result output i want to coffee
echo "<br/>";
$cola_factory = new ColaFactory();
$woman = $cola_factory->create();
$woman->buy();//Result output i want to cola
/**
 * Abstract factory pattern: provides a set of related or interdependent object interfaces to create
 * Note: The difference between this method and the factory method is a series, while the factory method is a series.
 * Interface Drink
 */
interface Drink{
    public function buy();
}

class CoffeeHot implements Drink {

    public function buy(){
        echo "i want to hot coffee";
    }

}

class CoffeeCold implements Drink {

    public function buy(){
        echo "i want to cold coffee";
    }

}

class ColaHot implements Drink{

    public function buy()
    {
        echo "i want to hot cola";
    }

}

class ColaCold implements Drink{

    public function buy()
    {
        echo "i want to cold cola";
    }

}

interface SuperFactory{
    public function createHot();
    public function createCold();
}

class CoffeeFactory implements SuperFactory {
    public function createHot()
    {
        return new CoffeeHot();
    }

    public function createCold()
    {
        return new CoffeeCold();
    }
}

class ColaFactory implements SuperFactory{
    public function createCold()
    {
        return new ColaCold();
    }

    public function createHot()
    {
        return new ColaHot();
    }
}

$coffee_factory = new CoffeeFactory();
$man = $coffee_factory->createHot();
$man->buy();

Design principles

1. Use more combinations and less inheritance
2. Programming for interfaces, not for implementations
3. Strive for loosely coupled design between interactive objects
4. Classes should be developed for extensions and closed for modifications
5. Depend on abstraction, not on concrete classes

Main points

1. All factories are used to encapsulate object creation
2. Simple factories, though not a real design pattern, are still a simple way to decouple client programs from specific classes.
3. The factory method uses inheritance to delegate the creation of objects to subclasses, which implement factory methods to create objects.
4. The abstract factory uses object composition, and the creation of objects is exposed by the existing factory interface.
5. The factory pattern used promotes loose coupling by reducing dependencies between application specific classes
6. Factory methods allow classes to defer instantiation to subclasses
7. Abstract factories create related object families without relying on their specific classes

Posted by bob_the _builder on Wed, 17 Apr 2019 12:09:34 -0700