Interview frequency: …
Short answer
Factory Method uses inheritance to create one kind of object through a dedicated method, while Abstract Factory uses composition to create whole families of related objects without specifying their concrete classes.
Explanation
With Factory Method, a class has a createProduct() method that lets derived classes choose the concrete product. Abstract Factory provides a full interface with several creation methods for a compatible family of products, such as a GUIFactory interface that creates Buttons and Checkboxes for different systems (WindowsFactory and MacFactory).
Code example
interface GUIFactory {
createButton(): Button;
createCheckbox(): Checkbox;
}
class WinFactory implements GUIFactory {
createButton() { return new WinButton(); }
createCheckbox() { return new WinCheckbox(); }
}Common mistakes
- Calling any static method that creates an object (such as User.create()) an Abstract Factory; a simple static creation method is called a Simple Factory.