[C# Design Patterns] The Builder Pattern

In this article, I’ll demonstrate how to use the Builder Design Pattern.

 

Video version here

 

What it is, and when to use it

The Builder Design Pattern is a “creational” pattern – you use it to create instances of objects.

You might want to use this pattern when you have a class with a large number of parameters in its constructors. Or, if the class has several different constructors, most of them with many parameters.

Another time you might use it is if the class does not have complex constructors, but has many properties that need to be set, before you can use some of its functions.

With the Builder pattern, you create a new function (or functions) that set some of the parameters, or properties, for you. This makes your calling code shorter, and easier to read and understand. It can also let you set consistent values for certain parameters, if your calling code needs them set a specific way, for certain types of similar objects.

 

Non-Pattern Version

Here is a class that could be used to build a report. It has five parameters in its constructor. However, a real report-generating class could easily have many more parameters.

Report.cs

 

This unit test method shows how you might use the class. You need to pass in all the parameters, every time you instantiate a Report object.

Notice that the last three parameters of the Tax reports are the same values. In this example, when we create a tax report, we always want to use those values for those parameters.

The commission reports also use their own (different) values for the last three parameters. Those are the “standard” values for those three parameters – for commission reports.

TestNonPatternReport.cs

 

 

Pattern Version

In this example, which uses the Builder Design Pattern, we have the same code for the Report class.

Report.cs

 

Now, we have a new ReportBuilder class. This class has methods that let us reduce the number of parameters needed to construct a Report object. It also sets the correct “standard” parameter values for the different types of reports – the Tax reports and the Commission reports.

ReportBuilder.cs

 

Now, to instantiate a Report object, we can call the methods in the ReportBuilder class, as shown in this unit test code.

TestPatternReport.cs

 

 

You don’t need to use a static method in the ReportBuilder class. That is just the way I chose to do it in this example.

You could also use a ReportBuilder class that is instantiated. You could pass values into its constructor, or set values on its public properties. Then, the builder class would use those values to “build” the class it is instantiating.

As long as you are simplifying object instantiation, by also reducing the parameters (or properties you need to set in your calling code), you are using the basic concept of the Builder Design Pattern.

 

Related patterns

Composition over Inheritance

Factory pattern

Wrapper/Façade Pattern

 

All my design pattern lessons

Source code for my design pattern lessons

2 thoughts on “[C# Design Patterns] The Builder Pattern

Leave a Reply

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