package Effective02.P12;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;
public abstract class Pizza {
public enum Topping{
HAM, MUSHROOM, ONION,PEPPER, SAUSAGE
}
final Set<Topping> toppings;
Pizza (Builder<?> builder){
toppings =builder.toppings.clone();
}
abstract static class Builder<T extends Builder<T>>{
EnumSet<Topping> toppings =EnumSet.noneOf(Topping.class); //用于创建elementType类型的空集
public T addTopping(Topping topping){
//Objects.requireNonNull()如果当前对象为空 会抛出一个空指针异常
//否则就会返回当前对象
toppings.add(Objects.requireNonNull(topping));
return self();
}
abstract NyPizza build();
protected abstract T self();
}
@Override
public String toString() {
return super.toString();
}
}
effective java 第三版 builder建造者 P12
最新推荐文章于 2022-06-01 19:00:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)