To avoid Constructor telescoping
and Bean setter which makes an object Immutable, Builder DP comes in to rescue
when it comes to lot of parameters during object creation.
package
com.javadsalgo.creationalDP;
public class
MovieTicketBooking {
/* Step 1- Make all fields
private final */
private final String
movie; // Name of the Movie
private final int totalTickets; //
Total tickets booked
private final int popcorn; // quantity of popcorn
private final int pizza; //
quantity of Pizza
private final int burger; //
quantity of burger
private final int coke; //
quantity of coke
private
MovieTicketBooking(Builder builder) {
movie = builder.movie;
totalTickets = builder.totalTickets;
popcorn = builder.popcorn;
pizza = builder.pizza;
burger = builder.burger;
coke = builder.coke;
}
public String
toString(){
return "Movie
Ticket Booked :\n"
+"Movie-"+ movie +"\n"
+"Total
Tickets-"+ totalTickets +"\n"
+"PopCorn-"+ popcorn +"\n"
+"Pizza-"+ pizza +"\n"
+"Burger-"+ burger +"\n"
+"Coke-"+ coke;
}
/*
* Step 2- Create inner class Builder which is
having same number of private fields as in outer class.
*/
public static class
Builder {
private final String
movie;
private final int totalTickets;
private int popcorn;
private int pizza;
private int burger;
private int coke;
/*
*
Step 3-In Builder class, create one constructor for all the required fields and create setter methods for optional fields
*/
public
Builder(String movie, int totalTickets) {
this.movie = movie;
this.totalTickets = totalTickets;
}
/*
*
Step 4- All this setter methods should return Builder class object to achieve method chaining
*/
public
Builder setPopcorn(int popcorn) {
this.popcorn = popcorn;
return this;
}
public
Builder setPizza(int pizza) {
this.pizza = pizza;
return this;
}
public
Builder setBurger(int burger) {
this.burger = burger;
return this;
}
public
Builder setCoke(int coke) {
this.coke = coke;
return this;
}
public
MovieTicketBooking build() {
return new
MovieTicketBooking(this);
}
}
}
CLIENT CALLING
package
com.javadsalgo.creationalDP;
public class
BuilderPatternExample {
public static void
main(String args[]) {
/*Creating object using Builder
pattern in java
Create MovieTickeBooking Object
using Builder object*/
MovieTicketBooking movieTicket =
new
MovieTicketBooking.Builder("3 Idiots", 5)
.setPopcorn(2)
.setBurger(3)
.setCoke(5)
.build();
System.out.println(movieTicket +"\n");
MovieTicketBooking movieTicketWithPopcornOnly
=
new
MovieTicketBooking.Builder("Dangal", 1)
.setPopcorn(2)
.build();
System.out.println(movieTicketWithPopcornOnly +"\n");
MovieTicketBooking movieTicketWithCokeAndBurger =
new
MovieTicketBooking.Builder("PK", 2)
.setBurger(3)
.setCoke(5)
.build();
System.out.println(movieTicketWithCokeAndBurger +"\n");
}
}
Output
Movie Ticket Booked :
Movie-3 Idiots
Total Tickets-5
PopCorn-2
Pizza-0
Burger-3
Coke-5
Movie Ticket Booked :
Movie-Dangal
Total Tickets-1
PopCorn-2
Pizza-0
Burger-0
Coke-0
Movie Ticket Booked :
Movie-PK
Total Tickets-2
PopCorn-0
Pizza-0
Burger-3
Coke-5
References