简体   繁体   中英

Factory Method Design Pattern -PHP

I am reading about factory design pattern, this pattern comes in 3 forms, simple factory, factory method and abstract factory.

what I understand, that's simple factory is not a design pattern, it just a type of encapsulate the object creation code in conjunction with if/switch statement or direct loading for the classes with exception if the class not exist!.

When I tried to move to factory method I can't find a clear enough example to defer it from simple factory.

could you guys give an example that's explain the factory method and how its defer from simple factory?

Thank you.

Simple Factory

A simple class with different methods (or one with a switch ) that is fully implemented with the creation logic. In PHP it can look like:

class AnimalFactory
{
    public function CreateAnimal($type)
    {
        if ($type == 'Dog') {
            return new Dog();
        } else {
            return new Cat();
        }
    }
}

If tommorow you'll have a new animal to support, you will have to change this class implementation. There are no subclasses here - this class knows everything about Animal creation.

Factory Method

Is what you use when you have a class that does some proccessing with Animal but doesn't know how or which Animal to create yet, therfore would like to have subclasses that will determine that. So we create a factory method that the sucblasses can override.

abstract Class MyAnimalProcess
{
    abstract function CreateAnimal();

    public function Process()
    {
        $animal = $this->CreateAnimal();
        ...
        ...
    }
}

class DogProcess extends MyAnimalProcess
{
    function CreateAnimal()
    {
        return new Dog();
    }
}

So, here you can have and abstract class that does something with an Animal , but let the subclasses of it determine which Animal it will be.

Abstract Factory

Take the factory method one step forward, and delegates the creation to another class - "the factory" class.
So it uses composition rather than inheritance.

abstract class AnimalFactory
{
    public abstract function CreatePet();
    public abstract function CreateWildAnimal();
}

class DogAnimalFactory extends AnimalFactory
{
    public function CreatePet()
    {
        return new Dog();
    }

    public function CreateWildAnimal()
    {
        return new AfricanWildDog();
    }
}

class CatAnimalFactory extends AnimalFactory
{
    public function CreatePet()
    {
        return new Cat();
    }

    public function CreateWildAnimal()
    {
        return new Tiger();
    }
}

class MyAnimalProcess
{
    function __construct($animalFactory) {
        $this->factory = $animalFactory;
    }

    public function ProcessPet()
    {
        $animal = $this->factory->CreatePet();
        ...
        ...
    }

    public function ProcessWild()
    {
        $animal = $this->factory->CreateWild();
        ...
        ...
    }
}

Maybe this will help, to make it more clear,
Abstract Factory is collection of the Factory Methods.

examples from real life:
Factory Method - plasticine/mold
Abstract Factory - cards factory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM