Creational PatternsAbstract Factory PatternMedium⏱️ ~3 min

Abstract Factory Structure and Participants

The Abstract Factory pattern involves four key participants that work together to create families of related objects while maintaining loose coupling.

Structural Components

«interface»
AbstractFactory
+ createProductA(): AbstractProductA
+ createProductB(): AbstractProductB
ConcreteFactory1
+ createProductA(): ProductA1
+ createProductB(): ProductB1
ConcreteFactory2
+ createProductA(): ProductA2
+ createProductB(): ProductB2

Participant Roles

AbstractFactory: Declares an interface with creation methods for each abstract product type. This interface defines the contract that all concrete factories must fulfill. Each method typically returns an abstract product type, not a concrete implementation.

ConcreteFactory: Implements the abstract factory interface to create concrete product objects. Each concrete factory corresponds to one product family variant. For example, WindowsFactory creates Windows-specific UI components, while MacFactory creates Mac-specific components.

AbstractProduct: Declares an interface for a type of product object. Each distinct product type in the family has its own abstract product interface (like Button and TextField in a UI toolkit).

ConcreteProduct: Implements the abstract product interface for a specific variant. Products created by the same concrete factory are designed to work together. For example, WindowsButton and WindowsTextField share the same visual style.

Client
- factory: AbstractFactory
+ useProducts(): void
Client uses only abstract interfaces, never concrete classes directly

Key Relationships

The inheritance relationship (▲) connects concrete factories to the abstract factory, and concrete products to their abstract product interfaces. The client has a composition relationship (◆) with the abstract factory, meaning it holds a reference to a factory instance but doesn't know which concrete factory it is.

Interview Tip: Emphasize that the client never calls new ConcreteProduct(). All object creation goes through the factory, which is the entire point of the pattern.
💡 Key Takeaways
AbstractFactory declares methods for creating each product type
ConcreteFactory implements methods to create products of one family variant
AbstractProduct defines interface for each type of product in the family
Client depends only on abstract types, not concrete implementations
Relationships use inheritance for factories and products, composition for client-factory
📌 Examples
1UI framework with abstract Button and TextField, concrete Windows and Mac factories
2Payment gateway with abstract PaymentProcessor and Receipt, concrete Stripe and PayPal factories
3Report generator with abstract Chart and Table, concrete PDF and HTML factories
← Back to Abstract Factory Pattern Overview