Thursday, March 30, 2017

Builder Design Pattern Using Java 8

INTRODUCTION

Builder design pattern is mainly focused on creating a complex object. Creation of complex object will be done by the sequence of smaller steps, i.e splitting the complex logic into multiple simple units. These simple units can be reused again. The whole complex object creation will be controlled by a controller/builder.
In this article, we are going to implementation the Builder Design Pattern using java before and after the version Java 8.

USECASE

Assembling a bike with its components like Engine, Wheel, and Gear etc.,

IMPLEMENTATION - BEFORE JAVA 8

Assembling a bike will have many activities like assembling the engine, wheel, estimating the cost and finally naming the bike etc,.
Lets us create Model Class - Bike.
public class Bike {
 private String name;
 private String wheelType;
 private String engineType;
 private int cost;
 //getters and setters
}


Create Builder interface with the list of activities.

public interface BikeBuilder {
 Void setName();
 void assembleEngine();
 void assembleWheel();
 void estimateCost();
 Bike getBike();
}

Now implement the BikeBuilder and create different bikes like HeroBike, RoyalEnfield Bike.

public class RoyalEnfield implements BikeBuilder {

 private Bike bike;

 RoyalEnfield() {
 bike = new Bike();
 }

 public void assembleWheel() {
 bike.setWheelType("Spoke");
 }

 public void estimateCost() {
 bike.setCost(170000);
 }

 public void assembleEngine() {
 bike.setEngineType("Diesel");
 }

 public void setName() {
 bike.setName("RoyalEnfield");
 }

 public Bike getBike() {
 return bike;
 }
}

Now time to test the builder pattern.

public class BuilderPatternMain {

public static void main(String[] args) {

 BikeBuilder heroBikeBuilder = new HeroBike();
 heroBikeBuilder.assembleWheel();
 heroBikeBuilder.assembleEngine();
 heroBikeBuilder.estimateCost();
 heroBikeBuilder.setName();

 // Get Hero Bike
 System.out.println(heroBikeBuilder.getBike().toString());

 BikeBuilder enfieldBike = new RoyalEnfield();
 enfieldBike.assembleWheel();
 enfieldBike.assembleEngine();
 enfieldBike.estimateCost();
 enfieldBike.setName();

 // Get Royal Enfield Bike
 System.out.println("\n" + enfieldBike.getBike().toString());
 }
}

IMPLEMENTATION - USING JAVA 8

New Java 8 Features like Lambda Expression, Streams, Method References etc., helps the developers to develop clear, easy understanding and simple code.
A separate package has been introduced for Functional Interfaces (java.util.function) as part of Java 8 release.
Here I am going to use Consumer Interface from java.util.function package ( to know more about Consumer Interface check out Functional Interface article).
No change in Bike.java  class.
  1. Return the HeroBike/RoyalEnfield object for assembleEngine(), assembleWheel(), estimateCost() and setName() methods.
  2. Modify the getBike() method, so that it will accept the Consumer object as an argument and also creates the HeroBike/RoyalEnfield object.

import java.util.function.Consumer;

public class HeroBike {

 private static Bike bike;

 public HeroBike assembleEngine() {
   bike.setEngineType("Petrol");
   return this;
 }

 public HeroBike assembleWheel() {
   bike.setWheelType("Alloy");
   return this;
 }

 public HeroBike estimateCost() {
   bike.setCost(50000);
   return this;
 }

 public HeroBike setName() {
   bike.setName("Hero");
   return this;
 }

 public Bike getBike(Consumer<HeroBike> heroBike) {
   bike = new Bike();

   HeroBike newHeroBike = new HeroBike();
   heroBike.accept(newHeroBike);

   return bike;
}
}

Check out below how easy is to implement the Builder Pattern using java 8.

public class BuilderPatternMainJava8 {

 public static void main(String[] args) {
   HeroBike heroBike = new HeroBike();
   System.out.println(heroBike.getBike(
     bike -> bike.assembleEngine()
                  .assembleWheel()
                  .estimateCost()
                  .setName())
                  .toString());

   RoyalEnfield royalEnfield = new RoyalEnfield();
   System.out.println("\n" + royalEnfield.getBike(
     bike -> bike.assembleEngine()
                 .assembleWheel()
                 .estimateCost()
                 .setName())
                 .toString());
 }
}

Here we have eliminated calling the method from the object again and again. Also, makes the code clear and easy understanding.

Hope this example clarifies the Builder Design Pattern Using Java 8. Please share your comments below.
Happy Knowledge Sharing!!!

No comments :

Post a Comment