The builder pattern will always give advice

Bob the Builder is a fairy tale that both me and many of my peers loved to watch at a time when we were talking “bep” on bread. Hearing about the builder’s pattern for the first time, it was the first thing I associated with it. Perhaps this pattern will not “do” to solve any problem, like the well-known and well-liked Bob, although it will find many uses.

Builder Pattern builder) belongs to the group of construction patterns. It is used to encapsuze the creation of a product and to enable its multi-step initialization. His class diagram is as follows:

UML diagram of master master classes
Figure. 1. UML diagram of master master classes

The abstract Builder class is responsible for providing the interface required to properly create the Product object. The ConcreteBuilder class (or classes) implement an abstract interface and create the right products. Optionally, the master structure is complemented by Director. It is responsible for commissioning the construction of products using the Builder object. It also controls the algorithm responsible for creating the final product object.

Since I’m a big fan of pizza, this example will be about it. There are many varieties of this simple and well-liked dish. We will create two different topping pizzas. We will also use the pattern variation of the builder without the manager’s class.

Let’s start by creating the Pizza class, which is our product. For simplicity, its attributes will be string. However, there is nothing to prevent them from being objects:

Pizza public class {
    String dough;
    String cheese;
    String meat;
    String mushrooms;
    String sauce;

    public String toString() {
        return "pizza ingredients:nDough - " + this.dough + ",nSauce: " + this.sauce + ",nCheese: " + this.cheese + ",nMeat: " + this.meat+"," + "n" + "Mushrooms: " + this.mushrooms;
    }
}

The next step will be to create a builder interface:

public interface AbstractBuilder {
    public void addDough(String dough);
    public void addCheese(String cheese);
    public void addMeat(String meat);
    public void addMushrooms(String mushrooms);
    public void addSauce(String sauce);
    public Pizza makePizza();
}

and the actual class of the builder:

pizzaBuilder implements AbstractBuilder {
    Pizza pizza = new Pizza();

    public void addDough(String dough) {
        pizza.dough = dough;
    }

    public void addCheese (String cheese) {
        pizza.cheese = cheese;
    }

    public void addMeat (String meat) {
        pizza.meat = meat;
    }

    public void addMushrooms(String mushrooms) {
        pizza.mushrooms = mushrooms;
    }

    public void addSauce(String sauce) {
        pizza.sauce = sauce;
    }

    public Pizza makePizza() {
        return pizza;
    }
}

Now our builder is ready to create new Pizza-class objects. Let’s test our solution by creating several types of delicious pizza:

PizzaBuilderTester public class {

    public static void main(String[] args) {

        AbstractBuilder italianPizzaBuilder = new PizzaBuilder();
        italianPizzaBuilder.addDough("italian");
        italianPizzaBuilder.addSauce("Marinara");
        italianPizzaBuilder.addCheese("Reggiano");
        italianPizzaBuilder.addMeat("chorizo");
        Pizza italianPizza = italianPizzaBuilder.makePizza();

        AbstractBuilder americanPizzaBuilder = new PizzaBuilder();
        americanPizzaBuilder.addDough("american");
        americanPizzaBuilder.addSauce("plum tomato");
        americanPizzaBuilder.addCheese("Mozzarella");
        americanPizzaBuilder.addMeat("ham");
        americanPizzaBuilder.addMushrooms("champaignon");
        AmericanPizza Pizza = americanPizzaBuilder.makePizza();

        System.out.println("Italian " + italianPizza);
        System.out.println("nAmerican " + americanPizza);
    }
}

The result of the code execution is as follows:

Italian pizza ingredients:
Dough - Italian,
Sauce: Marinara,
Cheese: Reggiano,
Meat: chorizo,
Mushrooms: null

American pizza ingredients:
Dough - American,
Sauce: plum tomato,
Cheese: Mozzarella,
Meat: ham,
Mushrooms: champaignon

The above example allows you to see the advantages of using the builder pattern. These are primarily:

  • creating objects in a multi-step procedure and not imposing this procedure (unlike factory patterns)
  • encapsulation of the operations necessary to create a complex object
  • possibility to exchange product implementations, thanks to the use of an abstract interface

In practice, this pattern is often used to build composite structures.

All the code from the entry is on my Github.

Sources: Eric Fre
[1]eman, Elisabeth Freeman, Kathy Sierra, Bert Bates: Design Patterns. Move your h
[2]ead! https://howtodoinjava.com/design-patterns/creational/builder-pattern-in-jav
[3]a/ http://www.blackwasp.co.uk/Builder.aspx

Leave a Comment

Your email address will not be published. Required fields are marked *