Design Patterns Java - Factory Method Php

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"


public class Creator
{
    private Registry registry;

    public Creator(Registry registry)
    {
        this.registry = registry;
    }

    public Product getProduct()
    {
        Product product = new Product1();
        this.registry.addProduct(product);

        return product;
    }
}


public class Client
{
    private static Creator creator = new Creator(new Registry());

    public static void main(String[] args)
    {
        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"