Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Clase Producto
- public class Pizza {
- private String tamaño;
- private String masa;
- private boolean orillaConQueso;
- private String ingredientes;
- // Constructor privado: solo se puede construir desde el Builder
- private Pizza(Builder builder) {
- this.tamaño = builder.tamaño;
- this.masa = builder.masa;
- this.orillaConQueso = builder.orillaConQueso;
- this.ingredientes = builder.ingredientes;
- }
- // Clase interna Builder
- public static class Builder {
- private String tamaño;
- private String masa;
- private boolean orillaConQueso;
- private String ingredientes;
- public Builder tamaño(String tamaño) {
- this.tamaño = tamaño;
- return this;
- }
- public Builder masa(String masa) {
- this.masa = masa;
- return this;
- }
- public Builder orillaConQueso(boolean orillaConQueso) {
- this.orillaConQueso = orillaConQueso;
- return this;
- }
- public Builder ingredientes(String ingredientes) {
- this.ingredientes = ingredientes;
- return this;
- }
- public Pizza build() {
- return new Pizza(this);
- }
- }
- public void mostrar() {
- System.out.println("Pizza: " + tamaño + ", " + masa + ", orilla con queso: " + orillaConQueso + ", ingredientes: " + ingredientes);
- }
- }
- // USO:
- public class Main {
- public static void main(String[] args) {
- Pizza pizza = new Pizza.Builder()
- .tamaño("Grande")
- .masa("Delgada")
- .orillaConQueso(true)
- .ingredientes("Queso, Pepperoni, Champiñones")
- .build();
- pizza.mostrar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment