SOLID & Design Patterns Interview Question #21

What is the Factory Pattern?

SOLID Principles, Design Fundamentals, Repository, Factory, Strategy & Decorator Junior Beginner

Quick Interview Answer

A factory encapsulates object creation so callers request an appropriate object without owning all construction logic. This is useful when creation depends on configuration, runtime input, multiple implementations, or complex setup.

Detailed Explanation

A factory encapsulates object creation so callers request an appropriate object without owning all construction logic. This is useful when creation depends on configuration, runtime input, multiple implementations, or complex setup.

A factory can improve OCP by moving selection and construction away from business code. Simple constructors should remain simple; a factory is unnecessary when object creation has no meaningful variation.

Code Example

public interface IReportExporter
{
    byte[] Export(Report report);
}

public interface IReportExporterFactory
{
    IReportExporter Create(string format);
}