SOLID & Design Patterns Interview Question #52

What is the difference between Factory Method and Abstract Factory?

Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate

Quick Interview Answer

Factory Method focuses on creating a product through a method whose concrete creation decision can vary. Abstract Factory exposes multiple creation operations for a family of related products.

Detailed Explanation

Factory Method focuses on creating a product through a method whose concrete creation decision can vary. Abstract Factory exposes multiple creation operations for a family of related products.

Use Factory Method when one product hierarchy needs flexible creation. Abstract Factory is more appropriate when several compatible product types must be selected together.

Code Example

// Factory Method
IExporter CreateExporter();

// Abstract Factory
public interface IStorageFactory
{
    IFileStore CreateFileStore();
    IMetadataStore CreateMetadataStore();
}