Builder Pattern

문지은·2022λ…„ 3μ›” 9일
0

λ””μžμΈνŒ¨ν„΄

λͺ©λ‘ 보기
4/4
post-thumbnail

문제 상황

μ—¬ν–‰ κ³„νšμ„ 짜고 μžˆλ‹€. μ—¬ν–‰ κ³„νšμ—λŠ” 총 μ—¬ν–‰ λ‚ μ§œμ™€ 각 λ‚ μ§œμ˜ 일정듀이 ν•„μš”ν•˜λ‹€. 그리고 이 μ—¬ν–‰ κ³„νšμ€ μ—¬ν–‰μžλ§ˆλ‹€ λ‹€λ₯΄κ³  μˆ˜μ‹œλ‘œ λ°”λ€” 수 μžˆμœΌλ―€λ‘œ μœ μ—°ν•œ ꡬ쑰가 ν•„μš”ν•˜λ‹€.

이 λ•Œ λΉŒλ” νŒ¨ν„΄μ„ μ‚¬μš©ν•  수 μžˆλ‹€.

μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄μ—μ„œ 반볡 μž‘μ—…μ„ λ³„λ„μ˜ 객체둜 μΊ‘μŠν™”ν–ˆλ˜ 것과 같이 λΉŒλ” νŒ¨ν„΄μ—μ„œ μ—¬ν–‰ κ³„νšν‘œλ₯Ό λ§Œλ“œλŠ” 일을 λ³„λ„μ˜ 객체둜 μΊ‘μŠν™”ν•œλ‹€.

client : λΉŒλ”μ—κ²Œ κ³„νšν‘œ λ§Œλ“€μ–΄λ‹¬λΌκ³  μš”μ²­
AbstractBuilder : κ³„νšν‘œλ₯Ό μƒμ„±ν•˜λŠ” λ©”μ„œλ“œλ₯Ό 가진 좔상 μΈν„°νŽ˜μ΄μŠ€
VacationBuilder : μ‹€μ œ μ œν’ˆμ„ λ§Œλ“€μ–΄μ„œ vacationμ΄λΌλŠ” 볡합 ꡬ쑰에 집어넣기

κ΅¬ν˜„ μ½”λ“œ

Abstract Builder

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;
	}
}

Vacation Builder

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;
	}
}

Client

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);
	}
}

μž₯점

  • 볡합 객체가 μƒμ„±λ˜λŠ” κ³Όμ • μΊ‘μŠν™”
  • νŒ©ν† λ¦¬ νŒ¨ν„΄μ—μ„œ ν•œ 단계에 μƒμ„±ν•˜λŠ” 것과 달리 μ—¬λŸ¬ 단계와 λ‹€μ–‘ν•œ 절차λ₯Ό κ±°μ³μ„œ 객체λ₯Ό λ§Œλ“€ 수 μžˆλ‹€.
  • μ œν’ˆ λ‚΄λΆ€ ꡬ쑰λ₯Ό ν΄λΌμ΄μ–ΈνŠΈλ‘œλΆ€ν„° 보호
  • ν΄λΌμ΄μ–ΈνŠΈμ™€μ˜ μ˜μ‘΄μ„±μ΄ μ•½ν•˜κΈ° λ•Œλ¬Έμ— κ΅¬ν˜„ μ½”λ“œ μ‰½κ²Œ μˆ˜μ • κ°€λŠ₯

단점

  • νŒ©ν† λ¦¬ νŒ¨ν„΄λ³΄λ‹€ 객체에 λŒ€ν•΄ 더 λ§Žμ€ 정보λ₯Ό μ•Œμ•„μ•Ό ν•œλ‹€.

ν•œμ€„ μš”μ•½

ν΄λΌμ΄μ–ΈνŠΈμ™€ 객체 κ°„μ˜ μ˜μ‘΄μ„±μ„ μ΅œμ†Œν™”ν•˜λ©΄μ„œ μœ μ—°ν•œ 볡합 객체 ꡬ쑰λ₯Ό κ΅¬μΆ•ν•˜κΈ° μœ„ν•΄ μ‚¬μš©

profile
λ°±μ—”λ“œ κ°œλ°œμžμž…λ‹ˆλ‹€.

0개의 λŒ“κΈ€