🎨 Design Patterns (Gang of Four)
Intermediate
What is the difference between Factory Method and Abstract Factory?
Answer
The Factory Method uses inheritance — a single factory method in a base class is overridden by subclasses to produce one type of product. It handles one product at a time and is about letting subclasses decide which class to instantiate. Abstract Factory uses composition — a factory interface produces families of related products through multiple factory methods. It coordinates the creation of multiple related objects that are designed to work together. Use Factory Method when you need one product and want subclasses to control which type; use Abstract Factory when you need to enforce that an entire family of products is used together (e.g., all Windows widgets or all Mac widgets, never mixed).