Tutorials

Wednesday, August 3, 2016

Factory Design Pattern

Hi, today I'm going to give you an explanation about the Factory Design Pattern. 

Design Patterns

Design patterns provide solutions for general problems that software developers face during their software developments. These solutions were obtained by the experienced object-oriented software developers as best practices to solve their problems. Design Patterns can be categorized for three main categories. They are,


  • Behavioral - Describe the interactions and focus on how communicate between the objects.
  • Creational - Create objects for a suitable class.
  • Structural - Construct larger structures from single parts (classes and objects).

Factory Pattern

Factory Design Pattern is one of the most popular design pattern in usage and it can be categorized under creational pattern. Using this design pattern , developers can create objects without exposing the creational method to the clients. As well as the developer can refer the newly created object using a common interface.

Understand Factory Pattern


We are going to discuss a simple example to understand the Factory Design Pattern. Here we are going to create Colors interface and  Red, Green and Blue concrete classes implementing the Colors interface. ColorsFactory class creates the Colors objects. DemonstrateFactoryPattern class is used ColorsFactory class to get the Colors objects. Below class diagram describes the Example that we are going to discuss.


If I explain simply about above class diagram, DemonstrateFactoryPattern class provide information about the object that needs ("Red/Green/Blue"). Then ColorsFactory class generates the object according to provided information.

Now let's see how implements the above example using Java.


  • Creates Colors interface


Colors.java

public interface Colors {
    public void generate();
}

  • Create concrete classes implementing the Colors interface
Red.java

public class Red implements Colors {
    @Override    public void generate() {
        System.out.println("This is Red Color");    }
}

Green.java

public class Green implements Colors {
    @Override    public void generate() {
        System.out.println("This is Green Color");    }
}

Blue.java

public class Blue implements Colors {
    @Override    public void generate() {
        System.out.println("This is Blue Color");    }
}


  • Create a Factory to generate objects according to provided information.


public class ColorsFactory {

//create getColor method to generate objects according to provided information
    public Colors getColor(String colorName) {

                if(colorName.equals("Red")){
              return new Red();          
  } else if(colorName.equals("Green")) {
              return new Green();        
  } else if(colorName.equals("Blue")) {
              return new Green();        
  } else return null;
    }
}

  • Create a class to pass information and get the object


DemonstrateFactoryPattern.java

public class DemonstrateFactoryPattern {

    public static void main(String[] args) {

        //create object of ColorsFactory        
 ColorsFactory colorsFactory = new ColorsFactory();

        //get an object of Red and call it's generate method.        
 Colors red = colorsFactory.getColor("Red");        
 red.generate();

        //get an object of Green and call it's generate method.        
 Colors green = colorsFactory.getColor("Green");        
 green.generate();

        //get an object of Blue and call it's generate method.        
 Colors blue = colorsFactory.getColor("Blue");        
 blue.generate();    }
}

  • Outputs

The outputs should be as below.

This is Red Color
This is Green Color
This is Green Color