Transport").Chair, Sofa, and CoffeeTable that all match the 'Victorian' style").The Concept: You define an interface for creating an object, but let subclasses decide which class to instantiate.
abstract create() method in the parent class. The child class overrides it.The Example (Logistics): You are building a logistics app.
LogisticsApp has a method planDelivery(). It needs a transport.RoadLogistics subclass overrides createTransport() → returns Truck.SeaLogistics subclass overrides createTransport() → returns Ship.Java Code:
abstract class Logistics {
// FACTORY METHOD: The parent doesn't know what comes out.
abstract Transport createTransport();
public void planDelivery() {
Transport t = createTransport(); // Call the factory method
t.deliver();
}
}
class RoadLogistics extends Logistics {
@Override
Transport createTransport() { return new Truck(); } // Decision made here
}
The Concept: You provide an interface for creating families of related or dependent objects without specifying their concrete classes.
createA, createB, createC). You pass this object around.The Example (Furniture Shop): You are selling furniture. A customer selects "Victorian Style".