Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use builder Design Pattern? [closed]

I'm learning about Design Patterns and found the Builder design pattern. What are the benefits of this design pattern and when should I use it? I surf www.dofactory.com and www.blackwasp.com but still don't understand the benefits. By the by, I'm new to design patterns so please explain me with simple ones.

like image 335
kevin Avatar asked Apr 26 '11 09:04

kevin


People also ask

When would you use a Builder design pattern?

This pattern should be used: When it's necessary to use a constructor with a long parameter list or when there's a long list of constructors with different parameters. When it's necessary to build different representations of the same object.

Why would one use the builder pattern?

Advantages of the Builder pattern include: Allows you to vary a product's internal representation. Encapsulates code for construction and representation. Provides control over steps of construction process.

When should design patterns not be used?

If a problem has two solutions, one that fits in ten lines of code, and another one with hundreds of lines of code along with a pattern, please consider not using the pattern. Their presence isn't a quality measurement.

What problem does the builder pattern solve?

Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.


2 Answers

The Builder Design Pattern helps us to slice the operations of building an object. It focuses on constructing a complex object step by step. It also enforces a process to create an object as a finished product. That means an object has to be massaged by some instructed steps before it is ready and can be used by others.

Generally, It allows you to encapsulate the complex create logic.

Builder pattern is very much like factory pattern. The key difference between a builder and factory is that a builder is useful when you need to do lots of things to build an object.

For more information:
http://en.wikipedia.org/wiki/Builder_pattern
http://www.codeproject.com/KB/architecture/Builder_Design_Pattern.aspx

like image 102
Kamyar Avatar answered Sep 19 '22 17:09

Kamyar


Builder design pattern helps to hide the complex logic in the builder class. Suppose we are developing a solution which constructs a large football field in different season as chosen by application users. We will create a builder class and pass the season info object in the builder class. Then the builder class is responsible construct the field according to seasons.

like image 25
Naseef Chowdhury Avatar answered Sep 17 '22 17:09

Naseef Chowdhury