Factory Design Pattern : A Creational Design Pattern

Factory design patterns provides an interface to create an object. Object creation is not just allocating some memory, in a complex systems the object creation will be a complex process. Setting certain defaults, following certain steps and initialisation of an object many more aspects will be taken care.

Let us take an example to understand it better, when we handle payment gateway transactions, a transaction object is not a simple object, it has much more complexities. It may be handling specific details for Master cards, Visa cards, or it may be related to income tax etc.

There are multiple ways of handling factory design patter, Factory Method pattern and Abstract Factory class are two different approaches.

Factory Method Pattern

Without a design pattern the code looks as fallow

processPayment() {
  switch(paymentGateway) {
    case 'RazorPay': 
       transaction = new RazorPayTransaction();
    break;
    case 'Paypal': 
       transaction = new PaypalTransaction();
    break; 
    case 'Paypal': 
       transaction = new StripeTransaction();
    break; 
  }
}

However the above code looks more cleaner when we apply design pattern the code looks as fallow

createRazorPayTransaction() {
  transaction = new RazorPayTransaction();
  return transaction;
}
createPaypalTransaction() {
  transaction = new PaypalTransaction();
  return transaction;
}
createTransactionFactory(paymentGateway) {
  switch(paymentGateway) {
    case 'RazorPay': 
       createRazorPayTransaction();
    break;
    case 'Paypal': 
       createPaypalTransaction();
    break; 
    case 'Paypal': 
    break; 
  }
}
processPayment() {
  transaction = createTransactionFactory('RazorPay');
}

the above code looks simple and we may not understand the significance of it with a small scale application. However the creational design pattern brings in lot of flexibility, like any complexity with respect to a particular payment gateway will be handled with respective creation method.

for example if I try to elaborate little about one particular method it may looks as below, and this shows how a particular method becomes complex while considering various cases.

createPaypalTransaction() {
  transaction = new PaypalTransaction();
   if (cardType === 'Master') {
     if (country === 'India') {
        if (cardKind === 'Chip') {

        }
      }
    }
  return transaction;
}

Abstract Factory Pattern

Abstract factory pattern simplifies and makes the pattern into more object oriented and enforces future extendability to follow certain strict guidelines by defining interfaces.

Taking an example of developing a super software to handle a robotic machines. There are various types of robotic machines like marine explorer, space explorer and terrain explorer. Its great to have abstract class which enforces and defines basic structure of the factory class. Let us see how does it look

abstract class RoboticMachineFactory {
  createCrawlingRobot();
  createRunningRobot();
  createFlyingRobot();
}

class SpaceExplorerRobotFactory extends RoboticMachineFactory {
  createCrawlingRobot();
  createRunningRobot();
  createFlyingRobot();
}

class TerrainExplorerRobotFactory extends RoboticMachineFactory {
  createCrawlingRobot();
  createRunningRobot();
  createFlyingRobot();
}

class MarineExplorerRobotFactory extends RoboticMachineFactory {
  createCrawlingRobot();
}

Read Classification of Design Patterns

One thought on “Factory Design Pattern : A Creational Design Pattern”

Leave a Reply to Classification of design patterns : Creational Design Patterns – Knowledge Cancel reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.