Design Patterns PHP - Abstract Factory Java

1) What have we changed ?

We created type "AbstractFactory" defining method for creating products


interface AbstractFactory
{
    /**
     * @return ProductA
     */
    public function createProductA();

    /**
     * @return ProductB
     */
    public function createProductB();

}




We created two classes "Factory1" and "Factory2" which implement interface "AbstractFactory" in such way that "Factory1" creates objects (1) and
"Factory2" creates objects of (2)


class Factory1 implements AbstractFactory
{

    /**
     * @return ProductA
     */
    public function createProductA()
    {
        return new ProductA1();
    }

    /**
     * @return ProductB
     */
    public function createProductB()
    {
        return new ProductB1();
    }
}

class Factory2
{
    /**
     * @return ProductA
     */
    public function createProductA()
    {
        return new ProductA2();
    }

    /**
     * @return ProductB
     */
    public function createProductB()
    {
        return new ProductB2();
    }
}




2) What are advantages of this solution ?

- we create in one place objects of appropriate factory
- in "Factories" code we have encapsulated creating products
- clients code is clear and readable and we needn't know nothing about concrete class of "Products"


$productA = $factory->createProductA();

$productB = $factory->createProductB();