Skip to main content

Posts

Showing posts from July, 2016

Builder Pattern : Java Code Example

Builder Pattern Builder Pattern is used when the increase of object constructor parameter combination leads to an exponential list of constructors. It is a solution to the telescoping constructor anti-pattern. Let's describe the pattern by using a sample requirement. Problem: We need to create a class for a Customer in a Bank and need following attributes in the Customer object. {Name, Father's Name, Date Of Birth, Mobile, Email, PAN, Permanent Address, Correspondence Address, Account, Branch} Constraint : Name,Date Of Birth,Account can not be null or empty There are 10 attributes in the objects So there will be total 2^10(1024) possible constructor and It is not practical to write all required constructors. One alternative could be to use Setter method but it will not work if any attribute has final modifier and would be difficult to fulfill the constraint that some attributes can not be null or empty. Now We will see how Builder Design Pattern helps to solve