Factory Design Pattern : A Creational Design Pattern

http://knowledge-cess.com/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

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

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

<code>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');
}</code>

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.

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

        }
      }
    }
  return transaction;
}</code>

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

<code>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();
}</code>

Read Classification of Design Patterns