Last active
May 28, 2021 19:44
-
-
Save pysoftware/ed75345de9fccd162ac1f155fe7b34a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Pizza { | |
public enum Topping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE} | |
final Set<Topping> toppings; | |
abstract static class Builder<V extends Pizza.Builder<V>> { | |
EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class); | |
public V addTopping(Topping topping) { | |
toppings.add(Objects.requireNonNull(topping)); | |
return self(); | |
} | |
abstract Pizza build(); | |
// Подклассы должны перекрывать этот метод, возвращая "себя" | |
protected abstract V self(); | |
} | |
Pizza(Builder<?> builder) { | |
toppings = builder.toppings.clone(); | |
} | |
} | |
class NyPizza extends Pizza { | |
private boolean sauceInside; | |
public static class Builder extends Pizza.Builder<Builder> { | |
private boolean sauceInside = false; | |
public Builder sauceInside() { | |
sauceInside = true; | |
return this; | |
} | |
@Override | |
NyPizza build() { | |
return new NyPizza(this); | |
} | |
@Override | |
protected Builder self() { | |
return this; | |
} | |
} | |
NyPizza(Builder builder) { | |
super(builder); | |
sauceInside = builder.sauceInside; | |
} | |
} | |
class test { | |
public static void main(String[] args) { | |
NyPizza nyPizza = new NyPizza.Builder() | |
.addTopping(Pizza.Topping.HAM) | |
.sauceInside() | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment