Web Dev Notes

AboutContact

Factory

The factory pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. The factory pattern is useful when you want to decouple the client code from the concrete implementations of objects.

Here's an example of how the factory pattern can be implemented in TypeScript:

// Product interface
interface Shape {
  draw(): void;
}

// Concrete products
class Circle implements Shape {
  draw() {
    console.log("Drawing a circle.");
  }
}

class Square implements Shape {
  draw() {
    console.log("Drawing a square.");
  }
}

// Factory class
class ShapeFactory {
  createShape(shapeType: string): Shape {
    if (shapeType === "circle") {
      return new Circle();
    } else if (shapeType === "square") {
      return new Square();
    } else {
      throw new Error("Invalid shape type.");
    }
  }
}

// Client code
const factory = new ShapeFactory();
const circle = factory.createShape("circle");
const square = factory.createShape("square");

circle.draw(); // Output: Drawing a circle.
square.draw(); // Output: Drawing a square.

In this example, we have a "Shape" interface that defines a "draw" method. We also have two concrete classes that implement the "Shape" interface: "Circle" and "Square".

We then create a "ShapeFactory" class that has a "createShape" method which takes a "shapeType" argument and returns an instance of the appropriate concrete class based on the value of "shapeType".

Finally, in the client code, we create an instance of the "ShapeFactory" class and use it to create instances of the "Circle" and "Square" classes by calling the "createShape" method.

This example demonstrates how the factory pattern can be used to decouple the client code from the concrete implementations of objects. The client code doesn't need to know the details of how the objects are created; it just needs to know what type of object it wants to create. The "ShapeFactory" class takes care of the rest.

Factory

The factory pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. The factory pattern is useful when you want to decouple the client code from the concrete implementations of objects.

Here's an example of how the factory pattern can be implemented in TypeScript:

// Product interface
interface Shape {
  draw(): void;
}

// Concrete products
class Circle implements Shape {
  draw() {
    console.log("Drawing a circle.");
  }
}

class Square implements Shape {
  draw() {
    console.log("Drawing a square.");
  }
}

// Factory class
class ShapeFactory {
  createShape(shapeType: string): Shape {
    if (shapeType === "circle") {
      return new Circle();
    } else if (shapeType === "square") {
      return new Square();
    } else {
      throw new Error("Invalid shape type.");
    }
  }
}

// Client code
const factory = new ShapeFactory();
const circle = factory.createShape("circle");
const square = factory.createShape("square");

circle.draw(); // Output: Drawing a circle.
square.draw(); // Output: Drawing a square.

In this example, we have a "Shape" interface that defines a "draw" method. We also have two concrete classes that implement the "Shape" interface: "Circle" and "Square".

We then create a "ShapeFactory" class that has a "createShape" method which takes a "shapeType" argument and returns an instance of the appropriate concrete class based on the value of "shapeType".

Finally, in the client code, we create an instance of the "ShapeFactory" class and use it to create instances of the "Circle" and "Square" classes by calling the "createShape" method.

This example demonstrates how the factory pattern can be used to decouple the client code from the concrete implementations of objects. The client code doesn't need to know the details of how the objects are created; it just needs to know what type of object it wants to create. The "ShapeFactory" class takes care of the rest.