Design Pattern in Java — Builder Pattern

It is time to learn another popular design pattern — builder patterns.

If you have done some projects in Java, there is high chance that you have already encountered the pattern. But what is it exactly and why we need this pattern? This article serves to help address those two questions.

As usual, let’s get started by an example. Suppose you opened a restaurant and sells delicious burger, and you have a great chef who invented a lot of different kinds of burger. Each burger will have different ingredients and stacks. Here, we take two kinds of burger as example:

  1. Beef burger: Buns, Cheese, Beef, lettuce.
  2. 2. Lettuce wrap chicken sandwich: lettuce wrap, tomatoes, Chicken

As a geek, you want to write a Java program to model the burger-building process.

Without design pattern, you might some Burger class looks like:

As you see, every time we want to build a burger, we need to call three setXXX() , this is not very efficient.

Now, since we want to build a burger, why do not we use builder pattern? Sounds like it can help. The first thing we want to do in builder pattern is to construct an abstract Builder class that simply captures all the steps of building process of your product — in this case, burger.

So, we write an abstract class called BurgerBuilder. The BurgerBuilder will contains three necessary piece of burger: bun, meat and something in-between.

Then, two concrete builder will be implemented: one is to make a beef burger and another is to make a chicken sandwich.

After we finished with builders, we need to have another role: director, or chef in our case. The chef will select what kind of burger to build by taking a builder instance as below:

You can think of the builder as the apprentices under the chef, but unlike chef, they can only master one kind of burger. The chef him/her self is just responsible for appointing person to do the actual work.

Now, the if we want to build beef burger, the code should look super concise:

Similarly, you can build chicken burger.

Simple UML diagram for Builder Pattern

Now, builder pattern is generally useful when you are trying to separate the different creation process from objects itself. In our case, we want to have burger, and both beef burger and chicken burger have three components, but the concrete logic of three components are distinct. This is why we try to apply builder pattern to this scenario.

Also, keep in mind that builder pattern has tons of implementations and mutations, and builder pattern can be used to form more delicate ways to solve more complex problems. For instance, if our chef invented a new category of burger that has more than three components, what do we do in this case? Say we add two components that not all burgers have like this:

Well, we can write a static inner class Builder like this:

In this way, we could use Builder class to control what part we want to add using chain calls like

This is much more concise.

Of course, there are a tons of learnings related to builder pattern not covered here, but it should be a good start for you. Again, happy learning and happy coding!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store