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.

III. Uses of the Prototype pattern include:

  1. When a client needs to create a set of objects that are alike or differ from each other only in terms of their state and it is expensive to create such objects in terms of the time and the processing involved.
  2. As an alternative to building numerous factories that mirror the classes to be instantiated (as in the Factory Method).
In such cases, the Prototype pattern suggests to:
  1. Create one object upfront and designate it as a prototype object.
  2. Create other objects by simply making a copy of the prototype object and making required modifications.
In the real world, we use the Prototype pattern on many occasions to reduce the time and effort spent on different tasks. The following are two such examples:
  1. New Software Program Creation: Typically programmers tend to make a copy of an existing program with similar structure and modify it to create new programs.
  2. Cover Letters: When applying for positions at different organizations, an applicant may not create cover letters for each organization individually from scratch. Instead, the applicant would create one cover letter in the most appealing format, make a copy of it and personalize it for every organization.
As can be seen from the examples above, some of the objects are created from scratch, whereas other objects are created as copies of existing objects and then modified. But the system or the process that uses these objects does not differentiate between them on the basis of how they are actually created. In a similar manner, when using the Prototype pattern, a system should be independent of the creation, composition and representation details of the objects it uses. One of the requirements of the prototype object is that it should provide a way for clients to create a copy of it. By default, all Java objects inherit the builtin clone() method from the topmost java.lang.Object class. The built-in clone() method creates a clone of the original object as a shallow copy. Furthermore, the Prototype specifies the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

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.