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:
Then we wanted to create it via a builder we could do:
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:
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.
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 updated