The Builder Design Pattern allows you to create a general guideline on how to create an object, then have different implementations on how to build parts of the object.

There are two principles in the Builder pattern; let's use an example of building an airplane to demonstrate the features:

The first principle is the general guideline that must be followed when building an object. For example, in building an airplane, the body must be constructed before the wings. This general guideline must be followed regardless of what type of airplane you are building.

The second principle are the different specifications on building the parts of the airplane. When building a jet airplane, the body must be built differently than a propeller airplane. These specifications are included in the pattern.

With these two principles in place, let's look at the UML of the Builder pattern using the example of building an airplane:

The Director class contains the logic of the general guideline. In this case, its BuildAirplane method would specify that an airplane body must be built before the wing. Therefore the code for the BuildAirplane method would be:

The IManufacture interface specifies the methods that all airplane manufactures must support. We see that it must be able to build the airplane body with the BuildBody method and build the wing with the BuildWing method. The GetProduct method just returns the product that is being built, which is the airplane.

The Manufacture class is the concrete manufacture class that has the implementation on building the parts of an airplane, hence it implements the IManufacture interface and holds a reference to the product variable, which is the airplane that is being built.

The Builder pattern allows you to create different concrete airplane manufactures that specify how the parts of the airplane are constructed. You can then pass in any manufacture to the director and it will build the airplane according to the specifications without having to change the client code.

You only need to determine the sequence in which the product (the airplane) is constructed in the Director.

You can have different implementation of the manufacturers on how the parts of an object (the body and the wings) are constructed.

The benefit of the Builder pattern is that you can swap out any implementation on how the parts are built by changing the manufactures, and the rest of the client code will not need to be changed.

In application frameworks today, we often see the Builder pattern being utilized. For example, you may have multiple configuration files that have information on database services, file location services, and notification services. These configuration files would be your manufactures, where each have their own specifications on how each part of the configuration object should be built. The director would specify the way to read the configuration file, for example, you may need to read the database services first before you read the notification services.

Below are the implementation code and the output from our example. Notice that we can pass in different airplane manufactures to the director and the airplane will be built according to the specifications:

Thanks for sharing your article, I liked it. I would like to propose an extra feature that you can add to the builder pattern that can create a more fluent api.

Your builder methods such as BuildBody and BuildWing are void methods. If you have these methods return an IManufacturer interface, which translates to "return this;" in your implementation the api could look like this.

m.BuildBody().BuildWing().GetProduct();

By changing the method name of BuildWing to WithWing it can get even more fluent.

Well, this is good, but I dont know why most of the online resource / example regarding of
the builder pattern are something like building any types of Vehicle.
Is that the pattern only applicable for Vehicle making. Let me share another example of this..............http://www.dofactory.com/Patterns/PatternBuilder.aspx#_self2[^]

Thanks
Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.