User user = User.build()
.id("aaa")
.name("aaa")
.password("password")
.build()
public class Employee {
private final String name; // 필수 인자
private final String salary; // 선택적 인자
private final String email; // 선택적 인자
// 필수 인자를 받는 생성자
public Employee(String name){
this(name,"연봉 비공개", "이메일 비공개");
}
// 1개의 선택적 인자를 받는 생성자
public Employee(String name, String salary){
this(name,salary,"이메일 비공개");
}
// 모든 인자를 다 받는 생성자
public Employee(String name, String salary, String email){
this.name = name;
this.salary = salary;
this.email = email;
}
// parameter가 무엇을 의미하는지 파악하기 어려움.
Order newOrder = new Order(1,200, 5000, 1,2);
Order newOrder = new Order();
newOrder.setId(1);
newOrder.setQuentity(200);
newOrder.setPrice(5000);
newOrder.setProduct(1);
newOrder.setStore(2);
public class Product {
private final String name;
private final int quentity;
private final int price;
private final String store;
private final String option;
public static class Builder {
// Required parameters(필수 인자)
private final String name;
private fianl int quentity;
private final int price;
// Optional Parameters - initialized to default values
private String store = "None";
private String option = "None";
public Builder(String name, int quentity, int price){
this.name = name;
this.quentity = quentity;
this.price = price;
}
public Builder store(String store){
store = store;
return this; // 이렇게하면 .으로 체인을 이어갈 수 있음.
}
public Builder option(String option){
option = option;
return this;
}
public Product build() {
return new Product(this);
}
}
private Product(Builder builder){
name = builder.name;
quentity = builder.quentity;
price = builder.price;
store = builder.store;
option = builder.option;
}
}
Product.bbuilder builder = new Product.builder('chip', 100, 2000);
builder.store('market');
builder.option('crispy');
Product prod = budiler.build();
Product prod = new Product.Builder('chip', 100, 2000)
.store('market')
.option('crispy')
.build(); // build()가 객체 생성해서 return
@Builder
public class Product {
private final String name;
private final int quentity;
private final int price;
private final String store;
private final String option;
}
public class Product {
private final String name;
private final int quentity;
private final int price;
private final String store;
private final String option;
}
@Builder
private Product(String name, int quentity, int price, String store, String option){
this.name = name;
this.quentity = quentity;
this.price = price;
this.store = store;
this.option = option;
Product.ProductBuilder builder = Product.builder();
builder.name('chip');
builder.quentity(100);
Product prod = builder.build();
Product prod = Product.builder()
.name('chip')
.quentity(100)
.build();
public interface Builder<T> {
public T build();
}
Ref:
https://johngrib.github.io/wiki/pattern/builder/
https://mangkyu.tistory.com/163