Builder Design Pattern, Still Useful?

Tags: Design Patterns

The builder design pattern is often employed to make object creation more readable and understandable when your class has a big old constructor with a long parameter list.   Rather than call that big constructor, the builder pattern allows the client to do something like this:

SampleClassBuilder sampleClassBuilder= new SampleClassBuilder();
SampleClass sampleClass = sampleClassBuilder
	.Prop1(100)
	.Prop2(200)
	.Prop4("hello")
	.Build();

I Don't Need That!

With the advent of C#'s class initializer syntax, all that extra code to achieve the above becomes quite useless when you can just to this:

			
SampleClass sampleClass2 = new SampleClass()
{
	Prop1 = 100, 
	Prop2 = 200, 
	Prop4 = "hello"
};

So is the builder design pattern no longer useful in C#? Not quite. What if you want your class to be immutable? Then your property setters are not accessible an you can't use the class initializer syntax. Well in that case you can use named parameters like this:

			
SampleClass sampleClass3 = new SampleClass(prop1: 100, prop2: 200, prop4: "hello");

Wait not so fast, you have to use every parameter in the constructor, unless you start making constructor overloads for every possible permutation of parameter lists you might need. That gets messy. So maybe the builder patter is useful after all albeit not very often. Here's how you might implement it.

Builder Pattern Example

	
public class SampleClass
{
	private int _prop1;
	private int _prop2;
	private int _prop3;
	private string _prop4;
	private string _prop5;

	public int Prop1
	{
		get { return _prop1; }
	}

	public int Prop2
	{
		get { return _prop2; }
	}

	public int Prop3
	{
		get { return _prop3; }
	}

	public string Prop4
	{
		get { return _prop4; }
	}

	public string Prop5
	{
		get { return _prop5; }
	}

	internal SampleClass(int prop1, int prop2, int prop3, string prop4, string prop5)
	{
		_prop1 = prop1;
		_prop2 = prop2;
		_prop3 = prop3;
		_prop4 = prop4;
		_prop5 = prop5;
	}
		
}

public class SampleClassBuilder
{
	private int prop1;
	private int prop2;
	private int prop3;
	private string prop4;
	private string prop5;

	public SampleClassBuilder Prop1(int value)
	{
		prop1 = value;
		return this;
	}
	public SampleClassBuilder Prop2(int value)
	{
		prop2 = value;
		return this;
	}
	public SampleClassBuilder Prop3(int value)
	{
		prop3 = value;
		return this;
	}
	public SampleClassBuilder Prop4(string value)
	{
		prop4 = value;
		return this;
	}
	public SampleClassBuilder Prop5(string value)
	{
		prop5 = value;
		return this;
	}

	public SampleClass Build()
	{
		return new SampleClass(prop1, prop2, prop3, prop4, prop5);
	}
}

So we have a class called SampleClass with immutable properties.  And we have a builder class called SampleClassBuilder with methods to set each property of SampleClass. Each method returns the current instance, enabling method chaining.  The Build method of SampleClassBuilder creates the instance of SampleClass by calling the its constructor with the long parameter list.

More Builder

The builder pattern is also useful for more complicated objects where you might have to build it in stages.  In that case your builder class becomes more like an ingredients list and a Director class sets the order that things get built.  More about the builder here.

No Comments

Add a Comment