The Builder Pattern is another creational pattern. It is used for creating different setups of complex aggregated objects. This sounds a lot more complex than what it really is.
One easy to understand parable I like is when you go to a fast food restaurant and order a hamburger meal. In order to create the meal, different parts need to be combined. There might be 5-10 different burgers, fries or salad, different soft drinks etc. Ok, so create (build) a meal, start off by creating a new HamburgerMealBuilder, then using that you define the type of burger, fries or salad, Coke or Fanta, and so on until you have the complete meal. There you have it, a perfect use of the builder pattern.
To understand the diagram better, take a look in my repository on github.And by the way, do not use the C# code from Wikipedia, it is incorrect (the [NotNull] attribute does not exist any more, and far worse, the CarBuilder does not Build new cars, it only hands of references to the same single one) I have updated the C# code on Wikipedia so hopefully it is usable now. The ConcreteBuilder should really create a new instance of the product.
One easy to understand parable I like is when you go to a fast food restaurant and order a hamburger meal. In order to create the meal, different parts need to be combined. There might be 5-10 different burgers, fries or salad, different soft drinks etc. Ok, so create (build) a meal, start off by creating a new HamburgerMealBuilder, then using that you define the type of burger, fries or salad, Coke or Fanta, and so on until you have the complete meal. There you have it, a perfect use of the builder pattern.
To understand the diagram better, take a look in my repository on github.
Kommentarer
Skicka en kommentar