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


  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube