Design Patterns – Factory Pattern – Part III

In this article, we will see how to use a factory pattern. Factory pattern is a creational type of design pattern, in short it provides a way to create objects. Another important point to note about this design pattern is that client who uses factory pattern is not aware of the implementation of factory pattern.

Even in our previous post Spring Boot REST CRUD API, we have used factory pattern to implement managers at service level. As part of this post, we will show another example of factory pattern. Factory pattern mainly used in cases when client just needs a class/object that can handle the job of doing the work at runtime without knowing any details of how it was implemented.

To show how to implement factory pattern, let’s assume we have different types of vehicles and we want to know what their maximum speed is.

Create an interface –

Our interface of vehicle will have a method to return a max speed of vehicle.


package com.betterjavacode.designpatterns.factoryexample;

public interface Vehicle {

void speed();

}

Now, we will have different classes (car, truck,bus,motorcycle) that will implement this interface to return their maximum speed. For article purposes, we will only be showing one class.


package com.betterjavacode.designpatterns.factoryexample;

public class Car implements Vehicle {

 public void speed() 

{
   System.out.println("Max Speed of this car is 100 mph");
}

}

To get an instance of an object, we will create a factory class that will return me appropriate instance of vehicle object based on what vehicle type I pass.


package com.betterjavacode.designpatterns.factoryexample;

public class VehicleFactory {

public Vehicle getVehicle(String vehicleType)
{
   if (vehicleType == null) 
   {
      return null;
}
   if (vehicleType.equalsIgnoreCase("car")) {
     return new Car();
   }
   if (vehicleType.equalsIgnoreCase("bus")) {
     return new Bus();
   }
   if (vehicleType.equalsIgnoreCase("motorcycle")) 
   {
     return new Motorcycle();
   }
   if ( vehicleType.equalsIgnoreCase("truck")) 
   {
     return new Truck();
   }
    return null;
}

}

A factory pattern demo class will get any of object of type vehicle at runtime.


package com.betterjavacode.designpatterns;

import com.betterjavacode.designpatterns.factoryexample.Vehicle;
import com.betterjavacode.designpatterns.factoryexample.VehicleFactory;

public class FactoryPatternDemo {

public static void getSpeed(String vehicleType) {
 VehicleFactory vehicleFactory = new VehicleFactory();

Vehicle veh1 = vehicleFactory.getVehicle(vehicleType);

veh1.speed();
 }

}

In this example, we have shown how to implement a design pattern type factory pattern. The code for this is available design patterns.

References

  1. Factory Pattern Example
  2. Factory Pattern
Advertisements

2 thoughts on “Design Patterns – Factory Pattern – Part III

  1. Pingback: Design Pattern – Abstract Factory Pattern – Part IV | Code Complete

  2. Pingback: Design Pattern – Prototype Pattern – Part VI | Code Complete

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s