class A{
public A(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
public A(int a, int b){
this(a,b,0);
}
}
A a = new A.Builder('ae').korean('에이').build();
NyPizza pizza = new NyPizza.Builder(SMALL).addTopping(SAUSAGE).addTopping(ONION).build();
Calzone calzone = new Calzone.Builder().addTopping(HAM).sauceInside().build();
public abstract class Pizza {
public enum Toping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE }
final Set<Topping> toppings;
abstract static class Builder<T extends Builder<T>>{
EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);
public **T** addTopping(Topping topping){
toppings.add(Objects.requireNonNull(topping));
return **self()**;
}
abstract Pizza build();
protected abstract T self();
}
Pizza(Builder<?> builder){toppings = builder.toppings.clone();}
}
위처럼 구현이 되어 있기 때문에, 상속 관계에서 빌더 패턴 적용에도 문제가 없다.
public abstract class Pizza {
public enum Toping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE }
final Set<Topping> toppings;
abstract static class Builder<T extends Builder<T>>{
EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);
public **Builder<T>** addTopping(Topping topping){
toppings.add(Objects.requireNonNull(topping));
return **this**;
}
abstract Pizza build();
protected abstract T self();
}
Pizza(Builder<?> builder){toppings = builder.toppings.clone();}
}
[Spring] Spring IoC Container, BeanScan, 생명주기, 스코프 (0) | 2024.11.20 |
---|---|
[자바] Collections API : TreeMap (0) | 2024.10.22 |
[Java] Java 메모리 구조, Reflection (0) | 2024.01.06 |
[Java] Exception, try-catch-finally, try-with-resources (0) | 2023.12.24 |
[데이터베이스]JPQL : N + 1 문제, Fetch Join, 주의사항 (0) | 2023.07.11 |