빌더 패턴은 복잡한 객체의 생성 과정과 표현 방법을 분리하여 다양한 구성의 인스턴스를 만드는 생성 패턴입니다.
public class Pizza {
private String dough;
private String sauce;
private String topping;
// 생성자 1
public Pizza(String dough) {
this(dough, null);
}
// 생성자 2
public Pizza(String dough, String sauce) {
this(dough, sauce, null);
}
// 생성자 3
public Pizza(String dough, String sauce, String topping) {
this.dough = dough;
this.sauce = sauce;
this.topping = topping;
}
}
이런 방식의 문제점:
public class Pizza {
private String dough;
private String sauce;
private String topping;
public Pizza() {}
public void setDough(String dough) { this.dough = dough; }
public void setSauce(String sauce) { this.sauce = sauce; }
public void setTopping(String topping) { this.topping = topping; }
}
이런 방식의 문제점:
public class Pizza {
private final String dough;
private final String sauce;
private final String topping;
private Pizza(Builder builder) {
this.dough = builder.dough;
this.sauce = builder.sauce;
this.topping = builder.topping;
}
public static class Builder {
// 필수 매개변수
private final String dough;
// 선택 매개변수
private String sauce = "";
private String topping = "";
public Builder(String dough) {
this.dough = dough;
}
public Builder sauce(String sauce) {
this.sauce = sauce;
return this;
}
public Builder topping(String topping) {
this.topping = topping;
return this;
}
public Pizza build() {
return new Pizza(this);
}
}
}
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class Pizza {
private final String dough;
private final String sauce;
private final String topping;
}
public class Order {
private final String customerName;
private final String address;
private final String phoneNumber;
private final String email;
private final String product;
private final int quantity;
private final boolean express;
private final String specialInstructions;
public static class Builder {
// 필수 매개변수
private final String customerName;
private final String address;
// 선택 매개변수 - 기본값으로 초기화
private String phoneNumber = "";
private String email = "";
private String product = "";
private int quantity = 1;
private boolean express = false;
private String specialInstructions = "";
public Builder(String customerName, String address) {
this.customerName = customerName;
this.address = address;
}
public Builder phoneNumber(String val) {
phoneNumber = val;
return this;
}
public Builder email(String val) {
email = val;
return this;
}
public Builder product(String val) {
product = val;
return this;
}
public Builder quantity(int val) {
quantity = val;
return this;
}
public Builder express(boolean val) {
express = val;
return this;
}
public Builder specialInstructions(String val) {
specialInstructions = val;
return this;
}
public Order build() {
return new Order(this);
}
}
private Order(Builder builder) {
customerName = builder.customerName;
address = builder.address;
phoneNumber = builder.phoneNumber;
email = builder.email;
product = builder.product;
quantity = builder.quantity;
express = builder.express;
specialInstructions = builder.specialInstructions;
}
}
사용 예제:
Order order = new Order.Builder("John Doe", "123 Street")
.phoneNumber("123-456-7890")
.email("john@example.com")
.product("Laptop")
.quantity(2)
.express(true)
.specialInstructions("Please deliver after 6 PM")
.build();
가독성이 좋음
유연성이 높음
불변성 확보
validating 용이
코드량 증가
생성 비용
A: 다음과 같은 경우에 빌더 패턴 사용을 고려해보세요:
A: 상황에 따라 다릅니다:
A: