design patterns
Factory Method
The Factory Method pattern defines an interface for creating an object but lets subclasses decide which class to instantiate. It defers instantiation to subclasses, promoting loose coupling. The key participants are the Creator (declares the factory method), ConcreteCreator (overrides the factory method to return a ConcreteProduct), Product (the interface for objects the factory method creates), and ConcreteProduct (implements the Product interface). Think of a logistics company: the parent company defines that packages must be delivered, but whether delivery happens by truck, ship, or drone is decided by regional subsidiaries. Use Factory Method when a class cannot anticipate the class of objects it must create, or when you want subclasses to specify the objects they create.
Use Cases
- •UI frameworks creating platform-specific components
- •Document editors supporting multiple file formats
- •Payment processing with different payment gateways
- •Notification systems sending via email, SMS, or push