Creational

Factory Method

Description

Defines an interface for creating an object, but let subclasses decide which class to instantiate. It defers instantiation to subclasses.

Where to use

  • Library frameworks.
  • \n
  • When exact types aren't known ahead of time.

Real World Example

A 'Logistics' class handles delivery. 'RoadLogistics' creates a 'Truck', 'SeaLogistics' creates a 'Ship'. The client just calls 'createTransport()'.

Code Example

class FactoryMethod:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Factory Method')