Builder design pattern (Fluent Interface in C++ and C#)

sukitha jayasinghe
Feb 28 · 4 min read

The builder design pattern is a simple but powerful creational pattern which decouples object construction from implementation by encapsulating complex object construction logic and delegate the construction responsibility to separate classes. The pattern is suitable for building multiple representations from the same object.

Builder pattern builds a complex object using simple objects and using a step by step approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. [by tutorialspoint]

Apply builder design pattern based on Code Smells:

  • Your class has a constructor with several optional arguments
  • Complex logic and multiple condition checks in the constructor
  • Some parameter combinations are disallowed (ex: continent and country)

Example:

This example demonstrates how to utilize builder patterns to construct email objects using multiple builder classes with a fluent interface. Here email object is constructed in multiple stages with header and body builder classes. Each builder has series of methods such as from() , to() and subject(). This example might look a little overengineered as the used email is not very complex.

email.h

This is the email class that has private variables like from, to, subject, body and attachment. Email class has a set of friend classes in-order for them to access the above private variables. The constructor is made private to prevent creating email objects directly from other classes.

email.cpp

operator (<<) overloaded to output variables in a user-friendly format.

abstractEmailBuilder.h

Base builder class has builder create methods such as header() and body() to get required builder implementation. It has a forward class declaration for EmailBodyBuilder and EmailHeaderBuilder. Cast operator for Email is also overloaded to return the final constructed email object ownership to the caller.

abstractEmailBuilder.cpp

emailBuilder.h

A concrete class implementation that initializes email object and passes it to the abstract class. This will allow Builder classes to access the object.

emailBodyBuilder.h

Demonstrate email body creation using EmailBodyBuilder concrete class.

emailHeaderBuilder.h

Demonstrate email header creation using EmailHeaderBuilder concrete class.

main.cpp

Conclusion

There are few variations of builder design pattern, this example is Fluent Interface like Implementation for c++. The common implementation is using directory-based class structure. There are lots of resources available online if you want to learn more.

The ideal time to apply design patterns is the refactoring phase. Code smells are the best way to find what are the changes required and which classes and codes segments require changes. I will write more stories related to code smells in the future.

Thank you Desmond Harris Fernando for reviewing the C# code.

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store