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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With