Implementing Creational Patterns in Java

Learn how to create a creational design pattern that is concerned about the process of object creation in Java from one or more classes.

A Creational design pattern is concerned about the process of object creation in Java from one or more classes. On the basis of a static and dynamic instantiation process, a creational pattern is clubbed into a class creational pattern and object creational pattern respectively. A class creational pattern uses inheritance as the mechanism of object instantiation; on the other hand, an object creational pattern delegates an object to create at run time. It addresses the issue of what objects are instantiated and in what manner.

The Group

The Creational patterns concept is an umbrella of five design patterns, namely,

Factory Method Pattern

Abstract Factory Pattern

Builder Pattern

Singleton Pattern

Prototype Pattern

Each pattern will be discussed in detail in this article.

Factory Method Pattern

This is the only class pattern under the Factory Method. User input decides which class to instantiate. When there are many subclass derived from a super class, the factory method pattern ensures that the specific subclass object is returned according to the user's choice.

Abstract Factory Pattern

The Abstract Factory Pattern is sommewhat similar to the factory method, but the difference is that the super class is abstract (cannot be instantiated directly) and it returns a factory object rather than returning an object of a specific class. This factory object can be used later to create objects at run time according to user choice.

Singleton Pattern

The Singleton pattern ensures that only one instance of a particular class is created per application. This pattern is useful where only one object instance is required, for example, while connecting to a database using only one object that manages the connection. Too many database connection objects are not only difficult to maintain, but also are adverse to performance.

Builder Pattern

The Builder Pattern simplifies creating a complex object in a one step at a time manner. It helps in separating the instantiation process of a complex object from its representation. This pattern is particularly useful in creating objects of the type given in Listing 13.

Prototype Pattern

The Prototype design pattern is used when we need a shallow copy of an object. Creating a new object is expensive in terms of memory and resources, so what we can do is make a clone of the object already available. Sometimes an exact replica of an object serves our purpose rather than creating a new object afresh. In Java, we can achieve this with the help of a Cloneable interface.

Conclusion

The Creational Pattern mainly focuses on object instantiation. There are certain overlapping circumstances where it may be difficult to choose an appropriate pattern. For example, the design decision between the Factory method pattern or Abstract factory pattern can be confusing at times. However, in some cases we can precisely decide which pattern better serves our purpose. A better understanding of the patterns can shed a lot of load from the shoulders of a code designer. These patterns evolved from programmers' toil and can make life easy once one gets a good grasp of the concept behind them.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.