Implementation

This time, the implementation part is very straightforward — we will use the Builder design pattern to implement the build process of McDonald’s burgers.

As you may know, McDonald’s menu contains multiple burgers (regular burger, cheeseburger, Big Mac just to name a few). All of these burgers use the very same products, just the ingredients list is different:

Regular burger — buns, beef patty, ketchup, mustard, grill seasoning, onions, pickle slices;

Cheeseburger — buns, beef patty, cheese, ketchup, mustard, grill seasoning, onions, pickle slices;

Big Mac — buns, cheese, beef patty, Big Mac sauce, grill seasoning, onions, pickle slices, shredded lettuce.

As you can see, by changing the build process of the burger (changing ingredients), we completely change the final result. Also, at any moment there could be a requirement to add a new burger to the menu. Finally, the user-friendly UI should be implemented where you can select a burger from the menu and see its price, ingredients and allergens list.

For this problem, the Builder design pattern is a great option since we can define different builder classes which build specific burgers. Also, if a new burger should be added to the menu at any point, we can simply introduce another builder class to cope with this change. Let’s check the class diagram first and then implement the pattern.

Class diagram

The class diagram below shows the implementation of the Builder design pattern:

Class Diagram — Implementation of the Builder design pattern

The Ingredient is an abstract class which is used as a base class for all the ingredient classes. The class contains allergens and name properties as well as getAllergens() and getName() methods to return the values of these properties.

There are a lot of concrete ingredient classes: BigMacBun, RegularBun, BeefPatty, McChickenPatty, BigMacSauce, Ketchup, Mayonnaise, Mustard, Onions, PickleSlices, ShreddedLettuce, Cheese and GrillSeasoning. All of these classes represent a specific ingredient of a burger and contains a default constructor to set the allergens and name property values of the base class.

Burger is a simple class representing the product of a builder. It contains the ingredients list and price property to store the corresponding values. Also, the class contains several methods:

addIngredient() — adds an ingredient to the burger;

getFormattedIngredients() — returns a formatted ingredients’ list of a burger (separated by commas);

getFormattedAllergens() — returns a formatted allergens’ list of a burger (separated by commas);

getFormattedPrice() — returns a formatted price of a burger;

setPrice() — sets the price for the burger.

BurgerBuilderBase is an abstract class which is used as a base class for all the burger builder classes. It contains burger and price properties to store the final product — burger — and its price correspondingly. Additionally, the class stores some methods with default implementation:

createBurger() — initialises a Burger class object;

getBurger() — returns the built burger result;

setBurgerPrice() — sets the price for the burger object.

BurgerBuilderBase also contain several abstract methods which must be implemented in the specific implementation classes of the burger builder: addBuns(), addCheese(), addPatties(), addSauces(), addSeasoning() and addVegetables().

BigMacBuilder, CheeseburgerBuilder, HamburgerBuilder and McChickenBuilder are concrete builder classes which extend the abstract class BurgerBuilderBase and implement its abstract methods.

BurgerMaker is director class which manages the burger’s build process. It contains a specific builder implementation as a burgerBuilder property, prepareBurger() method to build the burger and a getBurger() method to return it. Also, the builder’s implementation could be changed using the changeBurgerBuilder() method.

BuilderExample initialises and contains the BurgerMaker class. Also, it references all the specific burger builders which could be changed at run-time using the UI dropdown selection.

Ingredient

An abstract class which stores the allergens, name fields and is extended by all of the ingredient classes.

Concrete ingredients

All of these classes represent a specific ingredient by extending the Ingredient class and specifying an allergens’ list as well as the name value.

Big Mac Bun

Regular Bun

Cheese

Grill Seasoning

Beef Patty

McChicken Patty

Big Mac Sauce

Ketchup

Mayonnaise

Mustard

Onions

Pickle Slices

Shredded Lettuce

Burger

A simple class to store information about the burger: its price and a list of ingredients it contains. Also, class methods, such as getFormattedIngredients(), getFormattedAllergens() and getFormattedPrice(), returns these values in human-readable format.

BurgerBuilderBase

An abstract class which stores burger and price properties, defines some default methods to create/return the burger object and set its price. Also, the class defines several abstract methods which must be implemented by the derived burger builder classes.

Concrete builders

BigMacBuilder — builds a Big Mac using the following ingredients: BigMacBun, Cheese, BeefPatty, BigMacSauce, GrillSeasoning, Onions, PickleSlices and ShreddedLettuce.

CheeseburgerBuilder — builds a cheeseburger using the following ingredients: RegularBun, Cheese, BeefPatty, Ketchup, Mustard, GrillSeasoning, Onions and PickleSlices.

HamburgerBuilder — builds a cheeseburger using the following ingredients: RegularBun, BeefPatty, Ketchup, Mustard, GrillSeasoning, Onions and PickleSlices. AddCheese() method is not relevant for this builder, hence the implementation is not provided (skipped).

McChickenBuilder — builds a cheeseburger using the following ingredients: RegularBun, McChickenPatty, Mayonnaise and ShreddedLettuce. AddCheese() and addSeasoning() methods are not relevant for this builder, hence the implementation is not provided (skipped).

BurgerMaker

A director class which manages the burger’s build process and returns the build result. A specific implementation of the builder is injected into the class via constructor.

Example

First of all, a markdown file is prepared and provided as a pattern’s description:

BuilderExample initialises and contains the BurgerMaker class object. Also, it contains a list of BurgerMenuItem objects/selection items which is used to select the specific builder using UI.

The director class BurgerMaker does not care about the specific implementation of the builder — the specific implementation could be changed at run-time, hence providing a different result. Also, this kind of implementation allows easily adding a new builder (as long as it extends the BurgerBuilderBase class) to provide another different product’s representation without breaking the existing code.

As you can see in the example when a specific builder is selected from the dropdown list, a new product (burger) is created and its information is provided in the UI — price, ingredients and allergens.

All of the code changes for the Builder design pattern and its example implementation could be found here.