Design Patterns Java - Abstract Factory Php

1) What has changed ?

We add special parameter "type" to application, depending on which we create appropriate class instances.

java -Dtype=1 Main
java -Dtype=2 Main

In application we check parameter and create appropriate class instance


public class ClientA
{
    public ProductA createProductA()
    {
        switch (System.getProperty("type", "1"))
        {
            case "1":
                return new ProductA1();
            case "2":
                return new ProductA2();
        }

        return null;
    }
}

public class ClientB
{
    public ProductB createProductB()
    {
        switch (System.getProperty("type", "1"))
        {
            case "1":
                return new ProductB1();
            case "2":
                return new ProductB2();
        }

        return null;
    }
}




2) What are disadvantages of this solution ?

- Everywhere we need to pass special parameter "type"
- Creating object is complicated - switch instruction, we need to know concrete classes


3) How we can solve this problems

We can create special type "AbstractFactory" responsible only for creating objects of "Product"