Design Patterns PHP - Factory Method Java

1) What has changed ?

We created new object "Creator" - which implements creation of "Product" object and adding it to registry

In Client we easily can receive "Product" already added to "Registry"


class Creator
{
    /**
     * @var Registry
     */
    private $registry;

    function __construct(Registry $registry)
    {
        $this->registry = $registry;
    }

    public function getProduct()
    {
        $product = new Product1();
        $this->registry->addProduct($product);

        return $product;
    }
}


$product = $creator->getProduct();




2) We want to do more

In method "getProduct" - we create concrete implementation of "Product" namely "Product1"
What if we want create types "Products" with another implementation for example "Product2"

We must create new instance of "Creator" class which will create objects "Product2"

Let's create in step3 common abstract class "Creator" and two inheriting class: "Creator1" and "Creator2"