Design Patterns: Builder

Russell Hammett Jr. (Kritner)
Level Up Coding
Published in
5 min readJun 6, 2020

--

Photo by Nazarii Yurkov on Unsplash

The builder is a creational pattern that can be used to construct more complex objects, without having to new them up in calling code.

Though the builder pattern is not my most used creational pattern (that’s the Factory), it is one that I often rely on for the creation of more complex objects than what the factory easily allows for.

From Wikipedia:

The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.

I can’t think of an exceedingly straight forward example that truly shows of the power of the builder, but I am going to throw something together as a demonstration. Also note that a lot of the .net core app bootstrapping works around the concept of a builder, IHostBuilder comes immediately to mind.

Abstraction

public interface IAddress
{
string Address1 { get; }
string Address2 { get; }
string Address3 { get; }
string City { get; }
string State { get; }
string Zip { get; }
}

public interface IAddressBuilder
{
IAddressBuilder WithAddress1(string value);
IAddressBuilder…

--

--

Just a boring Application Developer/Dad. I enjoy gaming, learning new technologies, reading, and potentially other stuff. That’s about it.