DEV Community

Cover image for Creational Design Patterns
AryaGG
AryaGG

Posted on

Creational Design Patterns

  • Creational design pattern - ways to create objects.

  • These design patterns are used when a decision must be made at the time of instantiation of a class (i.e. creating an object of a class).

  • It provides a great deal of flexibility in what is created , who creates it, and how it is created

Image description

Factory Method Design Pattern

  • Factory design pattern defines an interface for creation objects, but allow sub classes to alter the type of objects that will be created.
  • The Factory Design Pattern allows you to delegate the responsibility of object creation to a factory class, instead of directly instantiating the class yourself in the client code.

Real-World Example:
Imagine you have are running a factory that produces different vehicles: cars, bikes & trucks. The process of manufacturing each type of vehicle is different. But the person ordering vehicle doesn't know the details how each vehicle is produced-they just need their vehicle.
Instead of exposing the complex logic of vehicle creation to the client, we use a factory method to handle the creation process for us. The client only interacts with a single interface, and the factory decides which specific type of vehicle to create.
Instead of directly creating concrete classes (like Car(),Bike()..) the factory method pattern provides an interface or abstract class with factory method that is implemented by concrete classes.

// Abstract Product (Vehicle)
class Vehicle {
  manufacture() {
    throw new Error("This method should be overridden by concrete products");
  }
}

// Concrete Products
class Car extends Vehicle {
  manufacture() {
    return "Manufacturing a Car.";
  }
}

class Bike extends Vehicle {
  manufacture() {
    return "Manufacturing a Bike.";
  }
}

class Truck extends Vehicle {
  manufacture() {
    return "Manufacturing a Truck.";
  }
}

// Abstract Factory
class VehicleFactory {
  createVehicle() {
    throw new Error("This method should be implemented by concrete factories");
  }
}

// Concrete Factories
class CarFactory extends VehicleFactory {
  createVehicle() {
    return new Car();  // Create a Car
  }
}

class BikeFactory extends VehicleFactory {
  createVehicle() {
    return new Bike();  // Create a Bike
  }
}

class TruckFactory extends VehicleFactory {
  createVehicle() {
    return new Truck();  // Create a Truck
  }
}

// Client code
function getVehicle(factory) {
  const vehicle = factory.createVehicle();  // Uses the factory method
  console.log(vehicle.manufacture());
}

// Using the factory to get different vehicles
const carFactory = new CarFactory();
const bikeFactory = new BikeFactory();
const truckFactory = new TruckFactory();

getVehicle(carFactory);   // Output: Manufacturing a Car.
getVehicle(bikeFactory);  // Output: Manufacturing a Bike.
getVehicle(truckFactory); // Output: Manufacturing a Truck.

Enter fullscreen mode Exit fullscreen mode

If you want to add new vehicle type eg: "Bus" you can add without changing the client code, making system more flexible and maintainable.

Top comments (0)