I am confused at the factory-method pattern.
The below code is from "https://www.oodesign.com/factory-method-pattern.html"
public interface Product { � } public abstract class Creator { public void anOperation() { Product product = factoryMethod(); } protected abstract Product factoryMethod(); } public class ConcreteProduct implements Product { � } public class ConcreteCreator extends Creator { protected Product factoryMethod() { return new ConcreteProduct(); } } public class Client { public static void main( String arg[] ) { Creator creator = new ConcreteCreator(); creator.anOperation(); } } Here is where I am confused :
Creator creator = new ConcreteCreator(); In the site, we apply this pattern in two cases
- when a class can't anticipate the type of the objects it is supposed to create
- when a class wants its subclasses to be the ones to specific the type of a newly created object
But in the client code, we put 'new' keword with ConcreteCreator (and I know this is the concrete factory for the concrete product).
Doesn't it mean that the client exactly know what type of object he/she need to create?
Can anyone help me?
Creatoris the contract how a product should be created in general.