Creational Patterns  «Prev 

Abstract Factory, Builder, and Prototype Design Patterns

I. Abstract Factory Pattern:

Abstract Factory Pattern: Factories and products are the key participants in the Abstract Factory pattern. This pattern captures how to create families of related product objects without instantiating classes directly. It is most appropriate when the number and general kinds of product objects stay constant, and there are differences in specific product families. We choose between families by instantiating a particular concrete factory and using it consistently to create products thereafter. We can also swap entire families of products by replacing the concrete factory with an instance of a different one. The Abstract Factory pattern's emphasis on families of products distinguishes it from other creational patterns, which involve only one kind of product object.
Abstract Factory
Abstract Factory pattern defines an interface for creating instances of several related abstract classes without specifying their concrete subclasses. The java.awt.Toolkit class used to create native peers for graphical user interface components is an example of an abstract factory class.

II. Builder Pattern

Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Builder Pattern builds different complex objects from the same set of component parts
Builder Pattern builds different complex objects from the same set of component parts. For example, the Director class might feed a long list of names and addresses into the Builder. The Builder might build an HTML page from that list, an XML page, or a PostScript file, depending on what type of Builder it was.


Prototype Pattern
The Prototype pattern creates new objects by copying a prototype object. By changing the object that is copied, you change the objectthat is created. This is useful when you need many different copies of a relatively complex object, all of which have the same initial state.