Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

factory method (1) vs factory(2) vs Builder (3) pattern

What is use cases for use (1),(2),(3). What is pro & cons to use it. What is difference between them?

like image 551
Ph0en1x Avatar asked Feb 05 '12 11:02

Ph0en1x


1 Answers

Factory Method pattern

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

  • Flexibility is important (low coupling)
  • Objects can be extended in subclasses
  • There is a specific reason why one subclass would be chosen over another—this logic forms part of the Factory Method.
  • A client delegates responsibilities to subclasses in parallel hierarchies.


Factory pattern or Simple Factory Pattern

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.


Builder Pattern

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

  • Construction of the object is not a simple task
  • Sub parts make up each object
  • There is a need for more than one kind of (with differencing sub-part in it) end-object is to be demanded by the client. This demand for differing end-objects may happen, if not at the same time, at least at different points of time.

More information

  • Design Patterns: Abstract Factory vs Factory Method
  • Questions about the Prototype Pattern
  • Builder Pattern vs Factory Pattern
  • Creational Patterns
  • Creational Design Patterns
like image 132
Devendra D. Chavan Avatar answered Oct 04 '22 15:10

Devendra D. Chavan