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 ?
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 ?
Factory pattern let you create an object at once at once while builder pattern let you break the creation process of an object. In this way, you can add different functionality during the creation of an object.
In my opinion Builder pattern is used when you want to create an object from a bunch of other objects and creation of part needs to be independent of the object you want to create. It helps to hide the creation of part from the client to make builder and client independent. It is used for complex objects creation (objects which may consists of complicated properties)
While factory pattern specifies that you want to create objects of a common family and you want it to be cerated at once. It is used for simpler objects.
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:
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
First some general things to follow my argumentation:
The main challenge in designing big software systems is that they have to be flexible and uncomplicated to change. For this reason, there are some metrics like coupling and cohesion. To achieve systems that can be easily altered or extended in its functionality without the need to redesign the whole system from scratch, you can follow the design principles (like SOLID, etc.). After a while some developer recognized that if they follow those principles there are some similar solutions that worked well to similar problems. Those standard solutions turned out to be the design patterns.
So the design patterns are to support you to follow the general design principles in order to achieve loosely coupled systems with high cohesion.
Answering the question:
By asking the difference between two patterns you have to ask yourself what pattern makes your system in which way more flexible. Each pattern has its own purpose to organize dependencies between classes in your system.
The Abstract Factory Pattern: GoF: “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”
What does this mean: By providing an interface like this the call to the constructor of each of the family’s product is encapsulated in the factory class. And because this is the only place in your whole system where those constructors are called you can alter your system by implementing a new factory class. If you exchange the representation of the factory through another, you can exchange a whole set of products without touching the majority of your code.
The Builder Pattern: GoF: “Separate the construction of a complex object from its representation so that the same construction process can create different representations.”
What does this mean: You encapsulate the process of construction in another class, called the director (GoF). This director contains the algorithm of creating new instances of the product (e.g. compose a complex product out of other parts). To create the integral parts of the whole product the director uses a builder. By exchanging the builder in the director you can use the same algorithm to create the product, but change the representations of single parts (and so the representation of the product). To extend or modify your system in the representation of the product, all you need to do is to implement a new builder class.
So in short: The Abstract Factory Pattern’s purpose is to exchange a set of products which are made to be used together. The Builder Pattern’s purpose is to encapsulate the abstract algorithm of creating a product to reuse it for different representations of the product.
In my opinion you can’t say that the Abstract Factory Pattern is the big brother of the Builder Pattern. YES, they are both creational patterns, but the main intent of the patterns is entirely different.
Builder Pattern and Factory pattern, both seem pretty similar to naked eyes because they both create objects for you.
But you need to look closer
This real-life example will make the difference between the two more clear.
Suppose, you went to a fast food restaurant and you ordered Food.
1) What Food?
Pizza
2) What toppings?
Capsicum, Tomato, BBQ chicken, NO PINEAPPLE
So different kinds of foods are made by Factory pattern but the different variants(flavors) of a particular food are made by Builder pattern.
Pizza, Burger, Pasta
Only Cheese, Cheese+Tomato+Capsicum, Cheese+Tomato etc.
Code sample
You can see the sample code implementation of both patterns here
Builder Pattern
Factory Pattern
From: http://www.oodesign.com/builder-pattern.html