What is the difference between the Builder design pattern and the Factory design pattern?
Which one is more advantageous and why ?
How do I represent my findings as a graph if I want to test and compare/contrast these patterns ?
A Factory Design Pattern is used when the entire object can be easily created and object is not very complex. Whereas Builder Pattern is used when the construction process of a complete object is very complex.
Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.
The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.
Builder Pattern: When to Use 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.
With design patterns, there usually is no "more advantageous" solution that works for all cases. It depends on what you need to implement.
From Wikipedia:
- Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
- Builder often builds a Composite.
- Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
- Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
Wikipedia entry for factory design pattern: http://en.wikipedia.org/wiki/Factory_method_pattern
Wikipedia entry for builder design pattern: http://en.wikipedia.org/wiki/Builder_pattern
A factory is simply a wrapper function around a constructor (possibly one in a different class). The key difference is that a factory method pattern requires the entire object to be built in a single method call, with all the parameters passed in on a single line. The final object will be returned.
A builder pattern, on the other hand, is in essence a wrapper object around all the possible parameters you might want to pass into a constructor invocation. This allows you to use setter methods to slowly build up your parameter list. One additional method on a builder class is a build() method, which simply passes the builder object into the desired constructor, and returns the result.
In static languages like Java, this becomes more important when you have more than a handful of (potentially optional) parameters, as it avoids the requirement to have telescopic constructors for all the possible combinations of parameters. Also a builder allows you to use setter methods to define read-only or private fields that cannot be directly modified after the constructor has been called.
Basic Factory Example
// Factory static class FruitFactory { static Fruit create(name, color, firmness) { // Additional logic return new Fruit(name, color, firmness); } } // Usage Fruit fruit = FruitFactory.create("apple", "red", "crunchy");
Basic Builder Example
// Builder class FruitBuilder { String name, color, firmness; FruitBuilder setName(name) { this.name = name; return this; } FruitBuilder setColor(color) { this.color = color; return this; } FruitBuilder setFirmness(firmness) { this.firmness = firmness; return this; } Fruit build() { return new Fruit(this); // Pass in the builder } } // Usage Fruit fruit = new FruitBuilder() .setName("apple") .setColor("red") .setFirmness("crunchy") .build();
It may be worth comparing the code samples from these two wikipedia pages:
http://en.wikipedia.org/wiki/Factory_method_pattern
http://en.wikipedia.org/wiki/Builder_pattern
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