What is use cases for use (1),(2),(3). What is pro & cons to use it. What is difference between them?
This pattern is very similar to the Factory Pattern, the client also asks the Factory for a specific type of object from a class hierarchy but the Create method of the factory class delegates the creation of the specific object to the derived classes and return the object of the class of the type asked by client. In essence, you have a single point of contact for the creation of several objects of a class hierarchy.
You can think of this as going to a airline ticket counter (controller) and asking for a ticket by giving your preference of the ticket type (first class, executive or economy). The user is not concerned with how the ticket is being generated, even though in an object representation the first class and the economy ticket are both derived from the base ticket class.
When to use
This pattern is very similar to the Factory method pattern. But unlike a factory method pattern, this pattern is a bit simpler. Instead of delegating the creation to the subclasses, the Create method of the Factory itself creates the instance of the required type and returns it.
In the builder pattern, the complex task of creation of objects is encapsulated in a class or method. For example, consider the case of ordering a meal at a fast food counter. The meal would typically consists of a burger, fries and a drink. Each item in the meal has its own creation process. Rather than the customer having to deal with the creation process of each item, this task is handled by the counter where one orders the meal. When the order is placed, the person as the counter takes charge of creating the meal that consists of the three items and returns the items as a single instance of a meal to the customer.
While another customer may ask for a meal that comes with large fries and diet coke. Again, the person at the order counter is responsible for building the order that was different than the first one. From the point of view of the customer, the order is always placed and the counter, which is followed by the meal being returned.
When to use
More information
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