Describe the builder design pattern



Describe the builder design pattern..

Answer / Sunil Kumar Rastogi

The Builder pattern separates the construction of a complex object from its representation. It allows the same construction process to create different representations (variations) of an object.nnThe Builder provides an interface for creating parts of a product, and it lets you produce complex objects step by step.nnHere is an example in JavaScript:n```javascriptnclass Director {n constructor() {n this._builder = null;n }nn setBuilder(builder) {n this._builder = builder;n }nn construct(n productType, n partsA, n partsB, n partsCn ) {n this._builder.reset();n this._builder.setPartA(partsA);n this._builder.setPartB(partsB);n this._builder.setPartC(partsC);n return this._builder.getProduct(productType);n }n}nnclass Builder {n constructor() {n this._product = null;n this._partA = null;n this._partB = null;n this._partC = null;n }nn reset() {n this._product = null;n this._partA = null;n this._partB = null;n this._partC = null;n }nn setPartA(part) {n this._partA = part;n }nn setPartB(part) {n this._partB = part;n }nn setPartC(part) {n this._partC = part;n }nn getProduct(productType) {n if (productType === 'ConcreteProduct1') {n this._product = new ConcreteProduct1();n } else if (productType === 'ConcreteProduct2') {n this._product = new ConcreteProduct2();n }n this._product.addPartA(this._partA);n this._product.addPartB(this._partB);n this._product.addPartC(this._partC);n return this._product;n }n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Design Patterns Interview Questions

What is universal design?

3 Answers  


What is singleton design pattern in java?

1 Answers  


What are the different types of Design Patterns and Explain them?

3 Answers   Genpact, Gradatim, Gul Ahmed, TCS, Wipro,


What is the difference between proxy and adapter?

1 Answers  


What are sequence diagrams, collaboration diagrams and difference between them ?

8 Answers   ACET, CT, CTS, Infosys,


What are the disadvantages of singleton pattern?

1 Answers  


Using any system, product, or service your organization provides, identify the human system roles for the product.

1 Answers  


How would you improve the UX design of our product?

1 Answers  


Where do you find inspiration and what are your long-term goals?

3 Answers  


Suppose we have file(ps), dont know how many records are there. Move half of the records to 2 files. How can we do?

1 Answers  


Did you use ooa/ood methodologies? Did you use design patterns?

0 Answers  


What are the design patterns and How can they make life easier for software development ?

1 Answers   ABC, HP,


Categories