DEV Community

The Builder Pattern .net core

Gary Woodfine on May 01, 2019

The Builder Pattern is a creational Gang of Four (GoF) design pattern, defined in their seminal book Design Patterns: Elements of Reusable Object...
Collapse
 
ihorholovatsky profile image
Lorderot

Great article, thanks!

I think that this example with 'Person' should be more complex,

Currently PersonBuilder.cs methods just assign properties, and method build just returns Person entity, so in this example, more relevant (I guess) is to create person entity and assign properties, see dotnetfiddle.net/Hu5nNb.

I understand that the idea of example if to show how to use 'Builder' pattern, but also I think it makes sense to show how pattern helps developers to design their code better, so I think it's better to show more complex example

Anyway, Thank You!

Collapse
 
gary_woodfine profile image
Gary Woodfine • Edited

No worries.

I understand where you're coming from, but also bear in mind that Complexity is a subjective and relative term. What, may be simple to you may be complex to others. So when devising a sample one has to err on the side of simplicity.

In the article, I have provided links to more complex implementations of the Builder pattern implementations.

I will be updating the article later with some examples of how to implement a Fluent implementations which may include further implementations.

Collapse
 
banarualexandru profile image
banarualexandru

Hi Gray

Thank you for the article.
I have a small remark for the last example. If we want to 'lock' the builder in order to force using the 'Create' method, I think we should implement a private constructor, for the builder, and make 'Create' method static. In this way, we will be sure that we will have at any moment an instance of Person. Because right now, if I will call 'new PersonBuilder().Occupation(_something).Build()' it will fail because there's no instance for the '_person' yet created.

Thanks
Good luck.

Collapse
 
gary_woodfine profile image
Gary Woodfine

Thanks for the feedback.

I will attempt to update the article to incorporate your input.

Thanks

Collapse
 
gary_woodfine profile image
Gary Woodfine

I'll try to update the article to include fluent aspects. Good idea!