Builder
The builder pattern has a small amount of overlap with the factory in that it will instantiate a type for you however it is slightly more intelligent in this regard.
Rather than you just requesting an instance you build up the instance (usually in a fluent style manner) then when you are done setting it up you would call the build method and it would be created.
So assuming we use have a class like:
1
public class Person
2
{
3
public string Name {get;set;}
4
public int Age {get;set;}
5
public bool IsMale {get;set;}
6
public List<string> Skills {get; set;}
7
8
public Person()
9
{
10
Skills = new List<string>();
11
}
12
}
Copied!
Then we wanted to create it via a builder we could do:
1
public class PersonBuilder
2
{
3
private string _name;
4
private int _age;
5
private bool _isMale;
6
private List<string> _skills;
7
8
public PersonBuilder Create()
9
{
10
_name = string.Empty;
11
_age = 0;
12
_isMale = false;
13
_skills = new List<string>();
14
return this;
15
}
16
17
public PersonBuilder WithName(string name)
18
{
19
_name = name;
20
return this;
21
}
22
23
public PersonBuilder WithAge(int age)
24
{
25
_age = age;
26
return this;
27
}
28
29
public PersonBuilder IsMale(bool isMale)
30
{
31
_age = age;
32
return this;
33
}
34
35
public PersonBuilder AddSkill(string skill)
36
{
37
_skills.Add(skill);
38
return this;
39
}
40
41
public Person Build()
42
{
43
return new Person
44
{
45
Name = _name,
46
Age = _age,
47
IsMale = _isMale,
48
Skills = _skills
49
};
50
}
51
}
Copied!
I am sure to begin with it looks like a lot of code, but basically we just expose every field in some logical way then populate what we care about and then build the last bit.
So here are a couple of examples:
1
var personBuilder = new PersonBuilder();
2
3
var maleMathTeacher = personBuilder.Create()
4
.WithName("Mr Green")
5
.WithAge(26)
6
.IsMale(true)
7
.AddSkill("math")
8
.AddSkill("teaching")
9
.Build();
10
11
var femaleGeographyTeacher = personBuilder.Create()
12
.WithName("Mrs Red")
13
.WithAge(26)
14
.AddSkill("geography")
15
.AddSkill("teaching")
16
.Build();
Copied!
This shows some basic usage but you can also do some more interesting stuff where you could create a builder for a specific subset and keep building without resetting.
1
var preSetBuilder = personBuilder.Create()
2
.WithAge(18)
3
.AddSkill("running")
4
.AddSkill("healing");
5
6
var recruit1 = preSetBuilder.WithName("Bob")
7
.IsMale(true)
8
.Build();
9
10
var recruit2 = preSetBuilder.WithName("Jane")
11
.IsMale(false)
12
.Build();
Copied!
It is a pretty useful pattern to know, it is very useful in automated tests or to wrap up lots of re-used instantiation logic as you can expose your data however you wish, be it a high level method which would populate the properties in a certain way. You can even add extension methods to allow you to have pre-generated versions.
One of the other things to take away from this pattern is the way they return the active object return this, which basically allows you to daisy chain the logic together and build up your object. This style of daisy chaining can be used elsewhere to great effect.
Last modified 2yr ago
Copy link