Skip to content

Creational patterns

brainatwork edited this page Mar 9, 2017 · 4 revisions

Builder

The builder pattern is an object creation software design pattern. Unlike the abstract factory pattern and the factory method pattern whose intention is to enable polymorphism, the intention of the builder pattern is to find a solution when the increase of object constructor parameter combination leads to an exponential list of constructors. Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

The builder pattern has another benefit. It can be used for objects that contain flat data (html code, SQL query, X.509 certificate...), that is to say, data that can't be easily edited. This type of data can't be edited step by step and must be edited at once.

Definition: The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so the same construction process can create different representations.

Advantages:

  • Allows you to vary a product’s internal representation.
  • Encapsulates code for construction and representation.
  • Provides control over steps of construction process.

Disadvantages:

  • Requires creating a separate ConcreteBuilder for each different type of Product.

References: Wikipedia

Factory pattern

Factory pattern is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Factory, as name suggest, is a place to create some different products which are somehow similar in features yet divided in categories. We create object without exposing the creation logic to the client and refer to newly created object using a common interface.

References: Tutorials Point

Clone this wiki locally