Elaborating on the Factory Design Pattern

Posted By : Sameer Grover | 29-Jun-2017

Factory Design Pattern :-

Factory design pattern is an implementation of a factory like in real world, this pattern comes under creation desing pattern category. In this design pattern we create a factory which is responsible to create our product as per client needs.

 

In this pattern we need to create a factory which will create an object car required by needs.

 

Where we can use that :-
We can use factory design pattern where we need a factory which is responsible for the object creation without any worry of how its create.


Lets create an example of factory design pattern :-

Firstly we create an object of our products say - car.
and provide its implementation.

/**
 * Create our product object - Car 
 *
 */
interface Car
{
   void displayCar();
}

/**
 * Implementation of Car - say i10. 
 */
class i10 implements Car
{
   private int price = 5;
   private int speed = 200;
   private String color;

   /** Constructor **/
   public i10( String color )
   {
      this.color = color;
   }

   /** getters/setters **/
   public int getPrice()
   {
      return price;
   }

   public void setPrice( int price )
   {
      this.price = price;
   }

   public int getSpeed()
   {
      return speed;
   }

   public void setSpeed( int speed )
   {
      this.speed = speed;
   }

   public String getColor()
   {
      return color;
   }

   public void setColor( String color )
   {
      this.color = color;
   }

   @Override
   public void displayCar()
   {
      System.out.println( "Car i10 : price - " + this.price + "lakhs speed - " + this.speed + " color - " + this.color );
   }

}

/**
 * Implementation of Car - say Swift. 
 *
 */
class Swift implements Car
{
   private int price = 4;
   private int speed = 220;
   private String color;

   /** Constructor **/
   public Swift( String color )
   {
      this.color = color;
   }

   /** getters/setters **/

   public int getPrice()
   {
      return price;
   }

   public void setPrice( int price )
   {
      this.price = price;
   }

   public int getSpeed()
   {
      return speed;
   }

   public void setSpeed( int speed )
   {
      this.speed = speed;
   }

   public String getColor()
   {
      return color;
   }

   public void setColor( String color )
   {
      this.color = color;
   }

   @Override
   public void displayCar()
   {
      System.out.println( "Car Swift : price - " + this.price + "lakhs speed - " + this.speed + " color - " + this.color);
   }

} 
        


Now we have two implementation of car. ( Swift & i10 )

Next step, create a factory which provides the instance of car that we need,

/** Factory of our product - Car **/
class CarFactory
{
   /**
    * Method used to create a car according to client color needs & car type.
    * @param carName
    * @param color
    * @return
    */
   public static Car createCar( String carName, String color )
   {
      if ( carName.equals( "Swift" ) )
         return new i10( color );
      else
         return new Swift( color );
   }
}


   /** client code **/
   public static void main( String[] args )
   {
      String carName = "i10";
      String color = "red";
      Car car = CarFactory.createCar( carName, color );
      car.displayCar();
   }
 
        

Thanks & Regards

Sameer Grover

About Author

Author Image
Sameer Grover

Sameer is an experienced Java developer with good working knowledge on Swing, Socket API, Collections, JDBC, Spring and Hibernate/JPA

Request for Proposal

Name is required

Comment is required

Sending message..