Wednesday 20 August 2014

Builder Design Pattern Implementation

Builder Design Pattern

For Builder design pattern implementation, at first we can proceed by creating a static nested class which will behave like a builder class and then will refer same variables which we are using in the outer class. We should provide separate methods to set the optional attributes in the Builder class and then we should declare a build method which will return the required object. Hence we should provide a private constructor by passing Builder class as an argument. Any details regarding Builder design pattern please refer below:

ElectronicDevice.java

package com.gaurav.designpattern.builder;

/**
 * @author gaurav
 * 
 */
public class ElectronicDevice {
public String processorSpeed;
public String storageCapacity;
private boolean isFingerPrintReaderAvailable;
private boolean isWifiAvailable;
private String deviceName;

private ElectronicDevice(ElectronicDeviceBuilder deviceBuilder) {
this.processorSpeed = deviceBuilder.processorSpeed;
this.storageCapacity = deviceBuilder.storageCapacity;
this.isFingerPrintReaderAvailable = deviceBuilder.isFingerPrintReaderAvailable;
this.isWifiAvailable = deviceBuilder.isWifiAvailable;
this.deviceName = deviceBuilder.deviceName;
}

public String getGetProcessorSpeed() {
return processorSpeed;
}

public String getGetStorageCapacity() {
return storageCapacity;
}

public String getDeviceName() {
return deviceName;
}

public boolean isFingerPrintReaderAvailable() {
return isFingerPrintReaderAvailable;
}

public boolean isWifiAvailable() {
return isWifiAvailable;
}

public static class ElectronicDeviceBuilder {

public String processorSpeed;
public String storageCapacity;
private boolean isFingerPrintReaderAvailable;
private boolean isWifiAvailable;
private String deviceName;

public ElectronicDeviceBuilder(String processorSpeed, String storageCapacity,
String deviceName) {
this.processorSpeed = processorSpeed;
this.storageCapacity = storageCapacity;
this.deviceName = deviceName;
}

public ElectronicDeviceBuilder setFingerPrintReaderAvailable(
boolean isFingerPrintReaderAvailable) {
this.isFingerPrintReaderAvailable = isFingerPrintReaderAvailable;
return this;
}

public ElectronicDeviceBuilder setWifiAvailable(boolean isWifiAvailable) {
this.isWifiAvailable = isWifiAvailable;
return this;
}

public ElectronicDevice build() {
return new ElectronicDevice(this);
}
}

@Override
public String toString() {
return "ProcessorSpeed = " + processorSpeed + ", StorageCapacity = "
+ storageCapacity + ", isFingerPrintReaderAvailable = "
+ isFingerPrintReaderAvailable + ", isWifiAvailable = "
+ isWifiAvailable + ", Device Name is = " + deviceName;
}
}

With the help of below test program we will know how to use builder class to get the object.

BuilderDesignPatternDemo.java

package com.gaurav.designpattern.builder;

/**
 * @author gaurav
 * 
 */
public class BuilderDesignPatternDemo {
public static void main(String[] args) {
ElectronicDevice electronicDevice1 = new ElectronicDevice.ElectronicDeviceBuilder(
"Intel Core i7 processor", "500GB", "Laptop")
.setFingerPrintReaderAvailable(true).setWifiAvailable(true)
.build();
System.out.println("First device system configuration details are : "
+ electronicDevice1);

ElectronicDevice electronicDevice2 = new ElectronicDevice.ElectronicDeviceBuilder(
"Intel Core i5-4570 Quad-Core", "1TB", "Desktop")
.setFingerPrintReaderAvailable(false).setWifiAvailable(false)
.build();
System.out.println("second device system configuration details are : "
+ electronicDevice2);

ElectronicDevice electronicDevice3 = new ElectronicDevice.ElectronicDeviceBuilder(
"1.2 GHz Quad Core Processor", "64GB", "Tablet")
.setFingerPrintReaderAvailable(false).setWifiAvailable(true)
.build();
System.out.println("Third device system configuration details are : "
+ electronicDevice3);

}
}

Result:-

First device system configuration details are : ProcessorSpeed = Intel Core i7 processor, StorageCapacity = 500GB, isFingerPrintReaderAvailable = true, isWifiAvailable = true, Device Name is = Laptop
second device system configuration details are : ProcessorSpeed = Intel Core i5-4570 Quad-Core, StorageCapacity = 1TB, isFingerPrintReaderAvailable = false, isWifiAvailable = false, Device Name is = Desktop
Third device system configuration details are : ProcessorSpeed = 1.2 GHz Quad Core Processor, StorageCapacity = 64GB, isFingerPrintReaderAvailable = false, isWifiAvailable = true, Device Name is = Tablet

No comments:

Post a Comment