Design Patterns PHP - Abstract Factory Java

1) What has changed ?

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

php main.php --type=1
php main.php --type=2

In application we check parameter and create appropriate class instance


$productA = null;

switch ($type) {
    case 1:
        $productA = new ProductA1();
        break;
    case 2:
        $productA = new ProductA2();
        break;
}

$productB = null;

switch ($type) {
    case 1:
        $productB = new ProductB1();
        break;
    case 2:
        $productB = new ProductB2();
        break;
}




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"