SOLID & Design Patterns Interview Question #22
What is the difference between Factory Method and Abstract Factory?
SOLID Principles, Design Fundamentals, Repository, Factory, Strategy & Decorator Junior Beginner
Quick Interview Answer
Factory Method defines a method for creating a product and often allows subclasses or implementations to decide which concrete product is created. Abstract Factory provides an interface for creating families of related objects without exposing their concrete classes.
Detailed Explanation
Factory Method defines a method for creating a product and often allows subclasses or implementations to decide which concrete product is created. Abstract Factory provides an interface for creating families of related objects without exposing their concrete classes.
Factory Method usually focuses on one product hierarchy, while Abstract Factory coordinates multiple related products that should be used together.
Code Example
public interface IUiFactory
{
IButton CreateButton();
IDialog CreateDialog();
}
// A WindowsUiFactory and WebUiFactory can each
// create a consistent family of UI components.