Design Patterns Java - Abstract Factory Php

1) What have we changed ?

We created type "AbstractFactory" defining method for creating products


public interface AbstractFactory
{
    ProductA createProductA();

    ProductB 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)


public class Factory1 implements AbstractFactory
{
    @Override
    public ProductA createProductA()
    {
        return new ProductA1();
    }

    @Override
    public ProductB createProductB()
    {
        return new ProductB1();
    }
}

public class Factory2 implements AbstractFactory
{
    @Override
    public ProductA createProductA()
    {
        return new ProductA2();
    }

    @Override
    public ProductB 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 "Product"


public class ClientA
{
    AbstractFactory factory;

    public ClientA(AbstractFactory factory)
    {
        this.factory = factory;
    }

    public ProductA getProductA()
    {
        return factory.createProductA();
    }
}

public class ClientB
{
    AbstractFactory factory;

    public ClientB(AbstractFactory factory)
    {
        this.factory = factory;
    }

    public ProductB getProductB()
    {
        return factory.createProductB();
    }
}