Builder Pattern in C#

Builder Pattern

Full code can be downloaded from here.


GOF definition –“Separate the construction of a complex object from its representation so that the same construction process can create different representations.” Builder pattern focuses on constructing a complex object step by step and returns the product as the final step. It creates one of the several targets.


The most common motivation for using Builder is to simplify the client code which creates the complex object. The main advantage will be client can still make the object but without knowing what actually is happening inside. Consider an example of creating different vehicles like car, motorcycle and bus. What builder helps us to simplify here is giving the client the complete vehicle with the only information about what is the type.  Client does not need to worry how the different vehicles are being created.


Builder contains the abstract interface for building the product. And there will be different ConcreteBuilder which has the different implantations of creating product. Director contains the instance of the Builder and constructs the product according to the Builder instance. Product is our final output which we need to create.


I have created the sample program which demonstrates the way to develop the different rules similar to the Microsoft Outlook. Full code can be downloaded from here.

Builder abstract class


abstract class RuleBuilder


protected Rule _rule;

public Rule Rule { get { return _rule; } }

public abstract void SetDescription();

public abstract void BuildCondition();

public abstract void BuildAction();


Product class

class Rule


IDictionary<string, string> _properties;

public Rule(string name)


_properties = new Dictionary<string, string>(0);

_properties["name"] = name;


public string this[string key]


get { return _properties[key]; }

set { _properties[key] = value; }


public void Show()


Console.WriteLine("------Rule {0}------", _properties["name"]);

Console.WriteLine("Description: {0}", _properties["description"]);

Console.WriteLine("Condition: {0}", _properties["condition"]);

Console.WriteLine("Result: {0}", _properties["action"]);



Director class

class MailManager


public RuleBuilder RuleBuilder { set; private get; }

public void CreateRule()







Note the RuleBuilder instance in MailManager. We can omit it with parameter in CreateRule method. I did above code so as to follow the class diagram.


——Rule Move Rule——

Description: Move the mail from one folder to another

Condition: When new mail arrived with title ‘xyz’

Result: Escape inbox and copy mail to ‘abc’ folder

——Rule Delete Rule——

Description: Delete the mail with specific condition

Condition: When new mail arrived with title ‘xxx’ OR mail arrived from address

Result: delete it


Builder is a type of Creational pattern like Factory but it gives us more control in each step of construction process. It is not a widely used pattern but it is an important one. Whenever we need to create different objects with some set of operations then we must think about Builder.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s