Ashish Patel: Notes

Atom feed

Recently added: Xunit, Publisher subscriber pattern, Request reply pattern, Middleware pattern, Command pattern

Factory Pattern

In factory pattern, we create objects without exposing the creation logic to the code that requires the object to be created.

// Factory.js

function deliveryFactory(address, item) {
  if (distance > 10 && distance < 50) {
    return new DeliveryByCar(address, item)
  }

  if (distance > 50) {
    return new DeliveryByTruck(address, item)
  }

  return new DeliveryByBike(address, item)
}

class DeliveryByBike {
  constructor(address, item) {
    this.address = address
    this.item = item
  }
}

class DeliveryByTruck {
  constructor(address, item) {
    this.address = address
    this.item = item
  }
}

class DeliveryByCar {
  constructor(address, item) {
    this.address = address
    this.item = item
  }
}

const newDelivery = deliveryFactory('121 baily ave, Toronto, canada', 'nitendo 360')

Abstract Factory Pattern

In factory pattern, we take care of creating objects of same family whereas in abstract factory pattern we will provide a constructor for creating families of related objects, without specifying concrete classes or constructors.

function abstractFactory(address, item, options) {
  if (options.isSameday) {
    return sameDayDeliveryFactory(address, item)
  }
  if (options.isExpress) {
    return expressDeliveryFactory(address, item)
  }

  return deliveryFactory(address, item)
}

Created 2020-08-21T15:56:27+05:18 · Edit