μ¬ν κ³νμ μ§κ³ μλ€. μ¬ν κ³νμλ μ΄ μ¬ν λ μ§μ κ° λ μ§μ μΌμ λ€μ΄ νμνλ€. κ·Έλ¦¬κ³ μ΄ μ¬ν κ³νμ μ¬νμλ§λ€ λ€λ₯΄κ³ μμλ‘ λ°λ μ μμΌλ―λ‘ μ μ°ν κ΅¬μ‘°κ° νμνλ€.
μ΄ λ λΉλ ν¨ν΄μ μ¬μ©ν μ μλ€.
μ΄ν°λ μ΄ν° ν¨ν΄μμ λ°λ³΅ μμ μ λ³λμ κ°μ²΄λ‘ μΊ‘μννλ κ²κ³Ό κ°μ΄ λΉλ ν¨ν΄μμ μ¬ν κ³ννλ₯Ό λ§λλ μΌμ λ³λμ κ°μ²΄λ‘ μΊ‘μννλ€.
client : λΉλμκ² κ³νν λ§λ€μ΄λ¬λΌκ³ μμ²
AbstractBuilder : κ³ννλ₯Ό μμ±νλ λ©μλλ₯Ό κ°μ§ μΆμ μΈν°νμ΄μ€
VacationBuilder : μ€μ μ νμ λ§λ€μ΄μ vacationμ΄λΌλ λ³΅ν© κ΅¬μ‘°μ μ§μ΄λ£κΈ°
public abstract class VacationBuilder {
String name;
List<Accommodation> accommodations = new ArrayList<Accommodation>();
List<String> events = new ArrayList<String>();
// κ³νμ μΆκ°νκ³ VacationBuilderλ₯Ό 리ν΄νλ λ©μλ
public abstract VacationBuilder addAccommodation();
public abstract VacationBuilder addAccommodation(String name);
public abstract VacationBuilder addAccommodation(String name, int year, int month, int day, int nights, int location);
public abstract VacationBuilder addEvent(String event);
// κ³νμ 리ν΄νλ λ©μλ μμ±
public Vacation getVacation() {
Vacation vacation = new Vacation();
vacation.setName(name);
vacation.setAccommodations(accommodations);
vacation.setEvents(events);
return vacation;
}
}
Outdoors Vacation Buillder
public class OutdoorsVacationBuilder extends VacationBuilder {
// μΌμΈ ν΄κ°μΌ κ²½μ°μ κ³ν μ§λ ꡬν
public OutdoorsVacationBuilder() {
this.name = "Outdoorsy Vacation Builder";
}
public VacationBuilder addAccommodation() {
this.accommodations.add(new Tent());
return this;
}
public VacationBuilder addAccommodation(String name) {
this.accommodations.add(new Tent(name));
return this;
}
public VacationBuilder addAccommodation(String name, int year, int month, int day, int nights, int location) {
Reservation reservation = new Reservation();
reservation.setArrivalDate(year, month, day);
reservation.setNights(nights);
Tent tent = new Tent(name);
tent.setReservation(reservation);
tent.setSiteNumber(location);
this.accommodations.add(tent);
return this;
}
public VacationBuilder addEvent(String event) {
this.events.add("Hike: " + event);
return this;
}
}
City Vacation Builder
public class CityVacationBuilder extends VacationBuilder {
// λμ ν΄κ°μΌ κ²½μ° κ³ν μ§λ λ©μλ ꡬν
public CityVacationBuilder() {
this.name = "City Vacation Builder";
}
public VacationBuilder addAccommodation() {
this.accommodations.add(new Hotel());
return this;
}
public VacationBuilder addAccommodation(String name) {
this.accommodations.add(new Hotel(name));
return this;
}
public VacationBuilder addAccommodation(String name, int year, int month, int day, int nights, int location) {
Reservation reservation = new Reservation();
reservation.setArrivalDate(year, month, day);
reservation.setNights(nights);
Hotel hotel = new Hotel(name);
hotel.setReservation(reservation);
hotel.setRoomNumber(location);
this.accommodations.add(hotel);
return this;
}
public VacationBuilder addEvent(String event) {
this.events.add("See the " + event + " show");
return this;
}
}
public class VacationDirector {
public static void main(String[] args) {
// ν΄λΌμ΄μΈνΈκ° λΉλμκ² κ³νν μμ±ν΄λ¬λΌκ³ μμ²
VacationBuilder outdoorsyVacationBuilder = new OutdoorsVacationBuilder();
Vacation outdoorsyVacation = outdoorsyVacationBuilder
.addAccommodation("Two person tent", 2020, 7, 1, 5, 34)
.addEvent("Beach")
.addAccommodation("Two person tent")
.addEvent("Mountains")
.getVacation();
System.out.println(outdoorsyVacation);
VacationBuilder cityVacationBuilder = new CityVacationBuilder();
Vacation cityVacation = cityVacationBuilder
.addAccommodation("Grand Facadian", 2020, 8, 1, 5, 0)
.addAccommodation("Hotel Commander", 2020, 8, 6, 2, 0)
.addEvent("Cirque du Soleil")
.getVacation();
System.out.println(cityVacation);
}
}
ν΄λΌμ΄μΈνΈμ κ°μ²΄ κ°μ μμ‘΄μ±μ μ΅μννλ©΄μ μ μ°ν λ³΅ν© κ°μ²΄ ꡬ쑰λ₯Ό ꡬμΆνκΈ° μν΄ μ¬μ©