C# Corner

The Builder Pattern in .NET

How to separate complex object construction from its representation using the Builder design pattern in C#.

The Builder Pattern is a common software design pattern that's used to encapsulate the construction logic for an object. This pattern is often used when the construction process of an object is complex. It's also well suited for constructing multiple representations of the same class. First, I'll go over the basics of the pattern, and then I'll provide an example to crystallize the concepts.

Core Concepts
The Builder Pattern is comprised of four components: a builder interface, a concrete builder, a director and a product. The builder interface defines a template for the steps to construct the product. The concrete builder implements the builder interface and provides an interface for getting the product. The director actually constructs the object through the builder interface. The product is the end object that's constructed.

Now that you understand the concepts of the pattern, I'll go over a full implementation of the pattern. My simple example encapsulates the process of brewing a beer. The first step is to define the Beer class, which is the product that will be built. The Beer class is a simplified representation of a beer that includes a label, volume, price and potency. I also added a ToString method override to concisely display the full contents of the object. See Listing 1 for the Beer class source code.

Next, I define the IBeerBuilder interface that specifies the necessary steps of constructing a beer object. I've also added a GetBeer method that returns the beer object at any time of the construction process. See Listing 2 for the full IBeerBuilder interface source code.

Now that I've defined the builder interface and the product, a concrete builder class that implements the IBeerBuilder interface is needed. In this case, I've created two beer builders, one for an amber ale (Listing 3) and one for a stout (Listing 4).

Fully Constructed Object
The final piece is to define a director class that will take a given IBeerBuilder interface and use it to execute the necessary steps to brew the beer. The BrewMaster class includes three methods: SetBeerBuilder, GetBeer and BrewBeer. The SetBeerBuilder class allows the consumer to set the IBeerBuilder interface to build the needed beer. The BrewBeer method executes the steps in order to fully brew the beer using the set IBeerBuilder, and the GetBeer method returns the fully constructed Beer object. See Listing 5 for the full BrewMaster director class source code.

Finally, to fully test the Builder Pattern implementation: First, create a new BrewMaster instance; then call the SetBeerBuilder method with a concrete IBeerBuilder object instance. Next, call the BrewBeer method on the BrewMaster instance. Lastly, call the GetBeer method on the BrewMaster to get the fully constructed Beer instance. The same BrewMaster instance can be used to create multiple Beer object representations as represented in Listing 6 and Figure 1.


[Click on image for larger view.]
Figure 1. Finished Builder Pattern Demo App

As you can see, the true strength of the Builder Pattern is that it lets you break up the construction of a complex object. Not only that, it also allows you to hide the construction process from the consumer, thus allowing for additional representations of the product to be added with ease. The pattern also encourages separation of concerns and promotes application extensibility.

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube