[Design Patterns] The Builder Pattern

Builder Pattern – The constructor is private. You create a nested static class called Builder. In the builder you define┬ámethods corresponding to the constructor parameters of the nesting class. Each method returns a Builder instance (return this at the end). Drawback is that you duplicate fields in the Builder. Finally you create a method called build in Builder which will call the private constructor of the nesting class and return the newly built nesting class.

Example:

 

One thought on “[Design Patterns] The Builder Pattern

Leave a Reply

Your email address will not be published. Required fields are marked *