> For the complete documentation index, see [llms.txt](https://grofit.gitbook.io/development-for-winners/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://grofit.gitbook.io/development-for-winners/development/general/design-patterns/builder.md).

# 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:

```csharp
public class Person
{
    public string Name {get;set;}
    public int Age {get;set;}
    public bool IsMale {get;set;}
    public List<string> Skills {get; set;}
    
    public Person()
    {
        Skills = new List<string>();
    }
}
```

Then we wanted to create it via a builder we could do:

```csharp
public class PersonBuilder
{
    private string _name;
    private int _age;
    private bool _isMale;
    private List<string> _skills;
    
    public PersonBuilder Create()
    {
        _name = string.Empty;
        _age = 0;
        _isMale = false;
        _skills = new List<string>();
        return this;
    }
    
    public PersonBuilder WithName(string name)
    {
        _name = name;
        return this;
    }
    
    public PersonBuilder WithAge(int age)
    {
        _age = age;
        return this;
    }
    
    public PersonBuilder IsMale(bool isMale)
    {
        _age = age;
        return this;
    }
    
    public PersonBuilder AddSkill(string skill)
    {
        _skills.Add(skill);
        return this;
    }
    
    public Person Build()
    {
        return new Person 
        {
            Name = _name,
            Age = _age,
            IsMale = _isMale,
            Skills = _skills
        };
    }
}
```

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:

```csharp
var personBuilder = new PersonBuilder();

var maleMathTeacher = personBuilder.Create()
	.WithName("Mr Green")
	.WithAge(26)
	.IsMale(true)
	.AddSkill("math")
	.AddSkill("teaching")
	.Build();

var femaleGeographyTeacher = personBuilder.Create()
	.WithName("Mrs Red")
	.WithAge(26)
	.AddSkill("geography")
	.AddSkill("teaching")
	.Build();
```

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.

```csharp
var preSetBuilder = personBuilder.Create()
	.WithAge(18)
	.AddSkill("running")
	.AddSkill("healing");

var recruit1 = preSetBuilder.WithName("Bob")
	.IsMale(true)
	.Build();

var recruit2 = preSetBuilder.WithName("Jane")
	.IsMale(false)
	.Build();
```

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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://grofit.gitbook.io/development-for-winners/development/general/design-patterns/builder.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
