In the previous two articles, we were talking about Builder Design Pattern and Fluent Builder With Recursive Generics. We recommend reading at least the first one for a better understanding of the Builder Design Pattern. The second one is the upgrade to the first article and if you want to learn more about using recursive generics with the Builder Pattern, then we recommend reading that one as well.

Why do we need the Faceted Builder?

Sometimes we may have a complex object, and the creational process requires more than one builder class. So, what we need to do is to introduce multiple builder classes in such a way, that we can jump from one builder to another while creating our object.

Faceted Builder approach helps us a lot in that process because we create a facade over our builders and it allows us to use all the builders to create a single object.

Let’s learn how to do that.

The source code is available at Faceted Builder – Source Code.

This article is divided into the following sections:

Faceted Builder Implementation

We are going to start with a “complex” object model:

We have the info part and the address part of our object, so we are going to use two builders to create this whole object.

As we said, we need a facade, so, let’s start with that:

We instantiate the Car object, which we want to build and expose it through the Build method.

What we need now is to create concrete builders. So, let’s start with the CarInfoBuilder which needs to inherit from the facade class:

As we can see, we receive, through the constructor, an object we want to build and use the fluent interface for building purpose.

Let’s do the same for the CarAddresBuilder class:

At this moment we have both builder classes, but we can’t start building our object yet because we haven’t exposed our builders inside the facade class. Well, let’s do that:

As of this moment, we can start building our object:

And that is it. We have built our object with the Faceted Builder approach.

Conclusion

With this article, we are finished with the small Builder Design Pattern series, but we have more articles to come about Design Patterns in C#. So stay tuned.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.