Factory Method is a creational design pattern that defines an interface for creating an object but lets subclasses decide which class to instantiate. Instead of calling a constructor directly with new, you call a factory method — and the subclass determines the concrete type.
The Problem It Solves
Imagine you're building a logistics app. Initially it only handles trucks. You hard-code new Truck() everywhere. Then the business needs sea shipping. Now you have to change every place that creates transport. Factory Method lets you isolate the creation logic so adding a new transport type requires only a new subclass.
Structure
Code Example
Step-by-Step: What Happens at Runtime
When to Use Factory Method
Factory Method in the Java Standard Library