Created
May 29, 2020 14:33
-
-
Save raviyasas/08b248b5e2cfae302d62a22e5401e822 to your computer and use it in GitHub Desktop.
Builder pattern - using inner class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.designPatterns.builder3; | |
public class Car { | |
private final String make; | |
private final Integer price; | |
private final Boolean sunroof; | |
private final Boolean alloyWheels; | |
private final Boolean cruiseControl; | |
private final Boolean electricSeats; | |
public Car(CarBuilder carBuilder){ | |
this.make = carBuilder.make; | |
this.price = carBuilder.price; | |
this.sunroof = carBuilder.sunroof; | |
this.alloyWheels = carBuilder.alloyWheels; | |
this.cruiseControl = carBuilder.cruiseControl; | |
this.electricSeats = carBuilder.electricSeats; | |
} | |
static class CarBuilder{ | |
private String make; | |
private Integer price; | |
private Boolean sunroof; | |
private Boolean alloyWheels; | |
private Boolean cruiseControl; | |
private Boolean electricSeats; | |
public Car build(){ | |
return new Car(this); | |
} | |
public CarBuilder(String make){ | |
this.make = make; | |
} | |
public CarBuilder price(Integer price){ | |
this.price = price; | |
return this; | |
} | |
public CarBuilder sunroof(Boolean sunroof){ | |
this.sunroof = sunroof; | |
return this; | |
} | |
public CarBuilder alloyWheels(Boolean alloyWheels){ | |
this.alloyWheels = alloyWheels; | |
return this; | |
} | |
public CarBuilder cruiseControl(Boolean cruiseControl){ | |
this.cruiseControl = cruiseControl; | |
return this; | |
} | |
public CarBuilder electricSeats(Boolean electricSeats){ | |
this.electricSeats = electricSeats; | |
return this; | |
} | |
} | |
@Override | |
public String toString() { | |
return "Car{" + | |
"make='" + make + '\'' + | |
", price=" + price + | |
", sunroof='" + sunroof + '\'' + | |
", alloyWheels='" + alloyWheels + '\'' + | |
", cruiseControl='" + cruiseControl + '\'' + | |
", electricSeats='" + electricSeats + '\'' + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.app.designPatterns.builder3; | |
public class CarApp { | |
public static void main(String[] args) { | |
Car.CarBuilder priusBuilder = new Car.CarBuilder("Prius"); | |
Car prius = priusBuilder.price(200).sunroof(true).alloyWheels(true).cruiseControl(true).electricSeats(true).build(); | |
System.out.println(prius); | |
Car.CarBuilder priusCBuilder = new Car.CarBuilder("Prius C"); | |
Car priusC = priusCBuilder.price(180).sunroof(true).alloyWheels(true).cruiseControl(false).build(); | |
System.out.println(priusC); | |
Car.CarBuilder priusVBuilder = new Car.CarBuilder("Prius V"); | |
Car priusV = priusVBuilder.price(180).alloyWheels(true).build(); | |
System.out.println(priusV); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment