Sunday, June 20, 2021

C# Design Pattern - Builder Design Pattern



Builder Design Pattern has following features
  1. It is a creational design pattern.
  2. It separates the construction of a complex object from its representation so that the same construction process can create different representations, depending upon the order of process steps and/or addition or deletion of a few process steps.
  3. Further, the steps of constructions should be independent of each other so that order of process steps can be changed. That is, the steps should not be tightly coupled.
  4. One possible solution is to create different processing steps or components of the products/classes and pass them as parameter to the constructor of the final product class. We can use Boolean parameters to decide which component will be used in the final product and null parameter to omit a component. But such constructor call becomes unwieldy. To overcome this, we use Builder Design Pattern.
  5. The products have common steps/processes/methods which are given inside abstract class or interface called Builder. These methods should be abstract and they should be overridden in the Concrete Builder classes that inherits/implements the Builder class. There are many Concrete Builder classes in BDP.
  6. The Director class is used in the BDP which defines the order in which to execute the building steps, while the Builder provides the implementation for those steps. 
  7. Having a director class in your program isn’t strictly necessary. We can always call the building steps in a specific order directly from the client code. However, the director class puts various construction routines so we can reuse them across the program.
  8. In addition, the director class completely hides the details of product construction from the client code. The client only needs to associate a builder with a director, launch the construction with the director, and get the result from the builder.
https://refactoring.guru/design-patterns/builder

No comments:

Post a Comment

Hot Topics