Factory Design Pattern

Factory Design Pattern - Learn From Examples

Factory Design Pattern – Learn From Examples

Factory Design Pattern is one of the most widely used design pattern in Software development. It comes under Creational Design Pattern and it has been used in many Java Frameworks.

This pattern specify the term “Code to Interface rather than Implementation”.

Factory Pattern takes care of objection object creation, it make the code loosely coupled because it completely eliminates the object creation from client side.

Here we will see the factory pattern by having an Interface and their implementations.

Interface:

Having an Interface IBiscuits with an abstract method makebiscuit.

Sub Classes:

We are having four classes (Britannia_Crackers, Britannia_Good_Day, Britannia_Bourbon, Britannia_Tiger) which implements the IBiscuits interface and each class has provided the implementation for makeBiscuit method.

Factory Class:

BiscuitFactory is a factory class in which we will get the Biscuit names and based on the input corresponding instance will be created and returned.

 

IBiscuits.java

package com.learnfromexamples.factory;
public interface IBiscuits {
 public void makeBiscuit();
}

Britannia_Bourbon.java

package com.learnfromexamples.factory;

public class Britannia_Bourbon implements IBiscuits{
 @Override
 public void makeBiscuit() {
 System.out.println("Hi I am BourBon Biscuit...");
 }
}

Britannia_Good_Day.java

package com.learnfromexamples.factory;

public class Britannia_Good_Day implements IBiscuits{
 @Override
 public void makeBiscuit() {
 System.out.println("Hi I am Good Day Biscuit...");
 }
}

Britannia_Milk_Bikis.java

package com.learnfromexamples.factory;

public class Britannia_Milk_Bikis implements IBiscuits{
 @Override
 public void makeBiscuit() {
 System.out.println("Hi I am Milk Bikis Biscuit...");
 }
}

Britannia_Tiger.java

package com.learnfromexamples.factory;

public class Britannia_Tiger implements IBiscuits{
 @Override
 public void makeBiscuit() {
 System.out.println("Hi I am Tiger Biscuit...");
 }
}

BiscuitNames.java

package com.learnfromexamples.factory;

public enum BiscuitNames {
 MILK_BIKIS, BOURBON, GOODDAY, TIGER;
}

BiscuitFactory.java

package com.learnfromexamples.factory;

public class BiscuitFactory {
 public static IBiscuits getInstance(BiscuitNames biscuitNames){
        IBiscuits biscuit = null;
        switch(biscuitNames){
              case MILK_BIKIS:
                   biscuit = new Britannia_Milk_Bikis();
                   break;
              case BOURBON:
                   biscuit = new Britannia_Bourbon();
                   break;
              case GOODDAY:
                   biscuit = new Britannia_Good_Day();
                   break;
              case TIGER:
                   biscuit = new Britannia_Tiger();
                   break;
              default:
                   break;
       }
       return biscuit;
   }
}

BiscuitFactoryImplementation.java

package com.learnfromexamples.factory;

public class BiscuitFactoryImplementation {

 public static void main(String[] args) {
 IBiscuits milkBikis = BiscuitFactory.getInstance(BiscuitNames.MILK_BIKIS);
 milkBikis.makeBiscuit();
 
 IBiscuits bourbon = BiscuitFactory.getInstance(BiscuitNames.BOURBON);
 bourbon.makeBiscuit();
 
 IBiscuits goodDay = BiscuitFactory.getInstance(BiscuitNames.GOODDAY);
 goodDay.makeBiscuit();
 
 IBiscuits tiger = BiscuitFactory.getInstance(BiscuitNames.TIGER);
 tiger.makeBiscuit();
 }

}

Output


Hi I am Milk Bikis Biscuit...
Hi I am BourBon Biscuit...
Hi I am Good Day Biscuit...
Hi I am Tiger Biscuit...

 

 

Leave a comment

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