20 minlesson

Introduction to Abstract Factory Pattern

Introduction to Abstract Factory Pattern

Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes.

The Problem

When you need to create related objects that must be used together:

typescript
1// Without Abstract Factory - inconsistent families
2function createAddressComponents(country: string) {
3 let formatter, validator, parser;
4
5 if (country === 'US') {
6 formatter = new USAddressFormatter();
7 validator = new UKAddressValidator(); // Bug! Wrong validator
8 parser = new USAddressParser();
9 }
10 // Easy to mix incompatible components
11}

The Solution: Abstract Factory

Ensure consistency by grouping related object creation:

typescript
1// Abstract Factory
2interface AddressFactory {
3 createFormatter(): AddressFormatter;
4 createValidator(): AddressValidator;
5 createParser(): AddressParser;
6}
7
8// Concrete Factory - all components are compatible
9class USAddressFactory implements AddressFactory {
10 createFormatter() { return new USAddressFormatter(); }
11 createValidator() { return new USAddressValidator(); }
12 createParser() { return new USAddressParser(); }
13}
14
15class UKAddressFactory implements AddressFactory {
16 createFormatter() { return new UKAddressFormatter(); }
17 createValidator() { return new UKAddressValidator(); }
18 createParser() { return new UKAddressParser(); }
19}

Pattern Structure

1┌─────────────────────────────┐
2│ AddressFactory │
3│ (Abstract Factory) │
4│ ─────────────────────────── │
5│ + createFormatter() │
6│ + createValidator() │
7│ + createParser() │
8└─────────────┬───────────────┘
9
10 ┌─────────┴─────────┐
11 ▼ ▼
12┌───────────────┐ ┌───────────────┐
13│USAddressFactory│ │UKAddressFactory│
14│───────────────│ │───────────────│
15│+ createFormatter│ │+ createFormatter│
16│+ createValidator│ │+ createValidator│
17│+ createParser() │ │+ createParser() │
18└───────┬───────┘ └───────┬───────┘
19 │ │
20 ▼ ▼
21 US Products UK Products
22 (Formatter, (Formatter,
23 Validator, Validator,
24 Parser) Parser)

Abstract Factory vs Factory Method

AspectFactory MethodAbstract Factory
CreatesOne productFamily of products
ViaInheritanceObject composition
FocusDefer to subclassEnsure compatibility
ComplexityLowerHigher

When to Use

  1. Product families - Objects must be used together
  2. Platform independence - Different implementations per platform
  3. Consistency - Prevent mixing incompatible components
  4. Configuration - Switch entire families at runtime

Benefits

  • Ensures product compatibility within families
  • Isolates concrete classes from client code
  • Easy to exchange product families
  • Promotes consistency across products

Summary

Abstract Factory is ideal when you need to create groups of related objects that must work together. It prevents mixing incompatible components and makes it easy to swap entire families.

Introduction to Abstract Factory Pattern - Anko Academy