Learn Creational Patterns in Software Design Patterns, including Factory, Singleton, Builder, and Prototype patterns with practical code examples.
TL;DR
-
Creational Patterns focus on how objects are created in software applications.
-
The Factory Pattern provides an approach for creating objects while deferring instantiation.
-
The Singleton Pattern restricts a class to a single instance.
-
The Builder Pattern separates the construction of complex objects from their representation.
-
The Prototype Pattern creates new objects by cloning an existing prototype.
This is Part 1 of the Series: Software Design Patterns. In this tutorial series, I would explain to you the important software design patterns you need to know. There are actually dozens of Software Design Patterns out there. However, many of them are just features natively supported by certain programming languages.
We would examine three categories of Software Design Patterns namely Creational Patterns, Structural Patterns and Behavioural Patterns.
In this part, we would focus on Creational Patterns and we would cover the following:
We would also try to give example with code snippet on how each is applied.
1. Factory Method Pattern
To understand the Factory Pattern, we first need to understand the concept of Factory in programming. A factory is simply an object for creating other objects. It could be a function that returns an object. Such function if called a factory function and it normally invokes the “new”.
For example, in a Spring application, we can have a factory function is given below that returns a RestTemplate object. This is shown below:
@Bean public RestTemplate getRestTemplate() { return new RestTemplate(); }
From the example above, if you need to want a new RestTemplate, you simply declare a private method of RestTemplate and annotate it with @Autowired annotation. Like so:
@Autowired private RestTemplate restTemplate;
Benefits of Factory Pattern
- subclasses of objects and redefine which classes to instantiate
- instantiation is deferred to subclases
2. Singleton Pattern
This pattern restricts the instantiation of a class to just a single instance. It ensures that
- only one instance of an object exists
- provides access to that instance
- controls creation of new objects
Singleton pattern has the benefit of keeping the global namespace clean and also permits lazy initialization. This is preferred to global variables. To implement singleton class, you will have to:
- declare all the constructors of the class as private. In this way, the class can’t be instantiated
- provide a static method that returns a reference to that instance
Example of a singleton class is given below:
public class Student { private static Student instance = new Student(); // eagerly loads the singleton private int count; private Student(){ //private to prevent instantiation from outside } public static Student getInstance(){ return instance; } public int getCount() { return count; } }
3. Builder Pattern
This is a pattern that breaks down the construction of complex object into several steps. The objective is to separate complex object creation from its representation. The Builder patter follows the approach:
- encapsulate the creation and assembling of various parts of a complex object into a separate Builder object
- object creation is delegated to a Builder object instead of creating the object directly
Since demonstrating this pattern is a bit more involved, you can find details in The Builder Pattern
4. Prototype Pattern
In the Prototype Pattern, new objects are created based on an existing prototype.
To implement this pattern, you need to first create a base abstract class. This class would specify a pure virtual clone() function. So classes can then derive from this abstract class implementing the clone() method.
// Prototype Base class public abstract class Shape implements Cloneable { public Shape clone() throws CloneNotSupportedException{ return (Shape) super.clone(); } } // Derived class Rectangle using the Prototype public class Rectangle extends Shape { @Override public Shape clone() throws CloneNotSupportedException { return (Rectangle)super.clone(); } } // Derived class Square using the Prototype public class Square extends Shape { @Override public Shape clone() throws CloneNotSupportedException { return (Square)super.clone(); } }
FAQs
What are Creational Patterns?
Creational Patterns are software design patterns that focus on object creation and provide different approaches for creating and managing objects.
What is the Factory Pattern?
The Factory Pattern uses a factory object or function to create other objects, allowing the details of object instantiation to be separated from the code that uses the object.
What is the Singleton Pattern?
The Singleton Pattern restricts the instantiation of a class to a single instance and provides a way to access that instance.
What is the Builder Pattern?
The Builder Pattern separates the construction of a complex object from its representation by delegating object creation to a Builder object.
What is the Prototype Pattern?
The Prototype Pattern creates new objects based on an existing object or prototype, typically by implementing a cloning operation.
Final Thoughts
Creational Patterns provide different approaches to managing object creation in software applications. Factory, Singleton, Builder, and Prototype each address object creation in a different way.
Understanding these patterns gives you a foundation for recognizing and applying common approaches to object creation. They are the first category in the broader Software Design Patterns series, followed by Structural and Behavioural Patterns.