Problem - Build a car which can have GPS, trip computer and various numbers of seats. Can be a city car, a sports car, or a cabriolet.
Solution -class Car {
int seats = 0;
boolean cityCar = false;
boolean cabriolet = false;
boolean sportCar = false;
boolean tripComputer = false;
boolean gps = false;
}
class CarBuilder {
int seats = 0;
boolean cityCar = false;
boolean cabriolet = false;
boolean sportCar = false;
boolean tripComputer = false;
boolean gps = false;
new CarBuilder() {
}
Car build() {
Car car = new Car();
car.seats = this.seats;
car.cityCar = this.cityCar;
car.cabriolet = this.cabriolet;
car.sportCar = this.sportCar;
car.tripComputer = this.tripComputer;
car.gps = this.gps;
}
CarBuilder withSeats(int seats) {
this.seat = seats;
return this;
}
CarBuilder withCityCar() {
this.cityCar = true;
return this;
}
CarBuilder withCabriolet() {
this.cabriolet = true;
return this;
}
CarBuilder withSportCar() {
this.sportCar = true;
return this;
}
CarBuilder withTripComputer() {
this.tripComputer = true;
return this;
}
CarBuilder withGPS() {
this.gps = true;
return this;
}
}
// Client
public class Client{
public static void main(String args[]){
Car car = new CarBuilder()
.withSeats(5)
.withCityCar()
.withCabriolet()
.withSportCar()
.withTripComputer()
.withGPS().build();
....
}
}
Also We should prefer getter setter with private accessiblity on class.