ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
FP
patterns · 2 min read

factory pattern

=====================================

=====================================

The factory pattern is a creational design pattern that provides an interface for creating objects without specifying the exact class of object that will be created. This allows developers to decouple object creation from specific classes, enabling more flexibility and extensibility in software design.

Problem


Imagine you're building an apiary platform where founders can command their bees to perform various tasks. You want to ensure that the platform is flexible enough to accommodate different types of commands (e.g., collecting nectar, pollinating flowers) without tightly coupling specific classes to each other. This is where the factory pattern comes in.

Solution


The factory pattern introduces an abstract interface or class that defines a common method for creating objects. Concrete subclasses then implement this interface to create specific types of objects. The client code interacts with the factory, requesting instances of objects without knowing (or caring) about the concrete classes involved.

Example Code: Apiary Command Factory

# Define an abstract command interface
interface Command {
  execute(): void;
}

# Implement a concrete command class for collecting nectar
class CollectNectarCommand implements Command {
  private beeId: string;

  constructor(beeId: string) {
    this.beeId = beeId;
  }

  execute(): void {
    console.log(`Bee ${this.beeId} is collecting nectar.`);
  }
}

# Implement a concrete command class for pollinating flowers
class PollinateFlowersCommand implements Command {
  private beeId: string;

  constructor(beeId: string) {
    this.beeId = beeId;
  }

  execute(): void {
    console.log(`Bee ${this.beeId} is pollinating flowers.`);
  }
}

# Define a factory class for creating command objects
class CommandFactory {
  createCommand(type: string, beeId: string): Command {
    switch (type) {
      case 'collect_nectar':
        return new CollectNectarCommand(beeId);
      case 'pollinate_flowers':
        return new PollinateFlowersCommand(beeId);
      default:
        throw new Error(`Unsupported command type: ${type}`);
    }
  }
}

In this example, the Command interface defines a common execute() method for all commands. The CollectNectarCommand and PollinateFlowersCommand classes implement this interface to create specific types of commands. The CommandFactory class uses a switch statement to create instances of these concrete command classes based on the requested type.

Benefits


The factory pattern provides several benefits, including:

  • Decoupling: Client code is decoupled from specific concrete classes, making it easier to add or remove functionality without affecting existing code.
  • Flexibility: The factory pattern enables you to create objects of different types without modifying the client code.
  • Extensibility: Adding new command types becomes straightforward by implementing a new concrete class and updating the factory.

Related


  • [Factory Method Pattern](#factory-method-pattern)
  • [Abstract Factory Pattern](#abstract-factory-pattern)

Sources


Frequently asked
What is factory pattern about?
=====================================
What should you know about problem?
Imagine you're building an apiary platform where founders can command their bees to perform various tasks. You want to ensure that the platform is flexible enough to accommodate different types of commands (e.g., collecting nectar, pollinating flowers) without tightly coupling specific classes to each other. This is…
What should you know about solution?
The factory pattern introduces an abstract interface or class that defines a common method for creating objects. Concrete subclasses then implement this interface to create specific types of objects. The client code interacts with the factory, requesting instances of objects without knowing (or caring) about the…
What should you know about example Code: Apiary Command Factory?
In this example, the Command interface defines a common execute() method for all commands. The CollectNectarCommand and PollinateFlowersCommand classes implement this interface to create specific types of commands. The CommandFactory class uses a switch statement to create instances of these concrete command classes…
What should you know about benefits?
The factory pattern provides several benefits, including:
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room