The Factory Method pattern defines a single method for creating one type of product, letting subclasses decide the concrete class, while the Abstract Factory pattern defines an interface for creating entire families of related products.
Key Points: • Factory Method centers on one product hierarchy; Abstract Factory centers on multiple related product hierarchies. • Abstract Factory is often implemented internally using several Factory Methods, one per product type. • Abstract Factory ensures the products it creates are compatible with each other, since they come from the same concrete factory. • Factory Method typically involves a single creator class or hierarchy; Abstract Factory involves a family of factory classes. • Choose Abstract Factory when you need to guarantee a consistent set of related objects, such as a UI theme.
Example: A Factory Method might just create a Shape given a type string, while an Abstract Factory for a cross-platform UI toolkit would create a matching Button, Checkbox, and Scrollbar together so the whole UI stays visually consistent for Windows or macOS.
Interview Tip: A concise interview answer is:
"Factory Method is about creating one product and delegating the exact class choice to subclasses, while Abstract Factory is about creating a whole family of related products through a single factory interface, guaranteeing the products it returns are compatible with each other."