Factory Method Pattern
Define a factory method in a base class and let subclasses decide which product to create.
The Factory Method Intent
Factory Method defines an interface for creating an object but lets subclasses decide which class to instantiate. It decouples the creation logic from the client code.
The Problem Without Factory Method
Without a factory, client code is littered with new ConcreteClass() calls. Adding a new variant means changing every call site — a violation of Open/Closed Principle.
// Client knows too much:
Shape shape;
if (type.equals("circle")) shape = new Circle(r);
else if (type.equals("rect")) shape = new Rectangle(w, h);
// Every new shape requires editing this code.