Abstract Factory

Create whole families of related objects through one interface, so code never mixes parts from different families.

Description

An abstract factory is an interface with one creation method per product: createBlobStore(), createQueue(), and so on. Each concrete factory builds one consistent family, such as AWS services or in-memory fakes for tests.

Client code receives a factory and asks it for what it needs. It never names a concrete class, so it can't accidentally pair an S3 store with a local queue. Switching the whole family is a one-line change where the factory is chosen.

Class diagram

uses«create»«create»ReportService+ publish(name, body)«interface»InfraFactory+ createBlobStore(): BlobStore+ createQueue(): JobQueue«interface»BlobStore+ put(key, data)LocalFactory+ createBlobStore()+ createQueue()AwsFactory+ createBlobStore()+ createQueue()S3Store+ put(key, data)SqsQueue+ enqueue(job)«interface»JobQueue+ enqueue(job)

Abstract Factory pattern class diagram

  1. Class ReportService with + publish(name, body).
  2. Interface InfraFactory with + createBlobStore(): BlobStore, + createQueue(): JobQueue.
  3. Interface BlobStore with + put(key, data).
  4. Class LocalFactory with + createBlobStore(), + createQueue().
  5. Class AwsFactory with + createBlobStore(), + createQueue().
  6. Class S3Store with + put(key, data).
  7. Class SqsQueue with + enqueue(job).
  8. Interface JobQueue with + enqueue(job).
  9. ReportService uses InfraFactory.
  10. LocalFactory implements InfraFactory.
  11. AwsFactory implements InfraFactory.
  12. S3Store implements BlobStore.
  13. SqsQueue implements JobQueue.
  14. AwsFactory creates S3Store.
  15. AwsFactory creates SqsQueue.

Sequence diagram

AppReportServiceAwsFactoryS3StoreSqsQueue1. new ReportService(aws)2. createBlobStore()3. S3Store4. createQueue()5. SqsQueue6. publish("q3-report.pdf")7. put(key, data)8. enqueue(job)

Abstract Factory pattern sequence diagram

  1. App → ReportService: new ReportService(aws).
  2. ReportService → AwsFactory: createBlobStore().
  3. ReportService receives "S3Store" back from AwsFactory.
  4. ReportService → AwsFactory: createQueue().
  5. ReportService receives "SqsQueue" back from AwsFactory.
  6. App → ReportService: publish("q3-report.pdf").
  7. ReportService → S3Store: put(key, data).
  8. ReportService → SqsQueue: enqueue(job).

TypeScript example

A report service that stores files and queues jobs. One choice of factory gives it either the AWS family or the in-memory family, never a mix. MemoryStore and MemoryQueue mirror the AWS classes in the diagram.

abstract-factory.ts
// Abstract Factory: one switch picks a whole family of infrastructure,
// so the app never mixes cloud and local pieces by accident.

// Product interfaces
interface BlobStore {
  put(key: string, data: string): string;
}
interface JobQueue {
  enqueue(job: string): string;
}

// The abstract factory: creates every product in the family
interface InfraFactory {
  createBlobStore(): BlobStore;
  createQueue(): JobQueue;
}

// Family 1: AWS
class S3Store implements BlobStore {
  put(key: string, data: string): string {
    return `s3://uploads/${key} (${data.length} bytes)`;
  }
}
class SqsQueue implements JobQueue {
  enqueue(job: string): string {
    return `sqs: ${job}`;
  }
}
class AwsFactory implements InfraFactory {
  createBlobStore(): BlobStore {
    return new S3Store();
  }
  createQueue(): JobQueue {
    return new SqsQueue();
  }
}

// Family 2: in-memory, for tests and local development
class MemoryStore implements BlobStore {
  readonly files = new Map<string, string>();
  put(key: string, data: string): string {
    this.files.set(key, data);
    return `memory://${key}`;
  }
}
class MemoryQueue implements JobQueue {
  readonly jobs: string[] = [];
  enqueue(job: string): string {
    this.jobs.push(job);
    return `memory queue: ${job}`;
  }
}
class LocalFactory implements InfraFactory {
  createBlobStore(): BlobStore {
    return new MemoryStore();
  }
  createQueue(): JobQueue {
    return new MemoryQueue();
  }
}

// The client only knows the interfaces
class ReportService {
  private readonly store: BlobStore;
  private readonly queue: JobQueue;
  constructor(infra: InfraFactory) {
    this.store = infra.createBlobStore();
    this.queue = infra.createQueue();
  }
  publish(name: string, body: string): string[] {
    const where = this.store.put(name, body);
    const job = this.queue.enqueue(`email link to ${name}`);
    return [where, job];
  }
}

export function demo(): string[] {
  const env: string = "production";
  const infra: InfraFactory = env === "production" ? new AwsFactory() : new LocalFactory();
  const cloud = new ReportService(infra).publish("q3-report.pdf", "%PDF-1.7 ...");
  const local = new ReportService(new LocalFactory()).publish("q3-report.pdf", "%PDF-1.7 ...");
  return [...cloud, ...local];
}

When to use it

Use it when

  • Several objects must be used together and come in matching sets: cloud vs local, light vs dark UI kit, vendor A vs vendor B.
  • You want to swap the whole set at once, for environments, tenants or tests.
  • Client code should depend on interfaces only, with one place that decides the concrete family.

Avoid it when

  • There is only one product. A single factory function, or Factory Method, is enough.
  • There is only one family and no realistic second one. The extra interfaces are speculative.
  • Families need to be mixed and matched freely. Inject each dependency separately instead.

Pitfalls

  • Adding a new product type means changing the factory interface and every concrete factory. Keep families small and stable.
  • Factories grow into a service locator that hands out everything. Keep each factory focused on one family.
  • Hiding which concrete classes are in use can make debugging harder. Log the chosen family at startup.

Alternatives

Factory Method

Choose it when only one kind of object needs creating, and subclasses decide which one.

Builder

Choose it when the hard part is assembling one complex object step by step, not choosing between families.

Dependency injection container

Choose it when you already wire dependencies through a container; register each family as a configuration profile.

Prototype

Choose it when new objects are best made by copying configured examples instead of calling constructors.