The Singleton Pattern

April 12, 2023
The last blog post was a general overview of the fundamentals of Object-Oriented programming. I would like to go over a specific pattern that commonly appears in software engineering. One of the most common and simplest patterns to understand is the Singleton pattern. The Singleton pattern is quite simply an object-oriented pattern that involves the creation and invocation of only one instance of a class.

What is the singleton pattern?

In the programming language Java, only the package that contains the Singleton’s class can create the instance of the singleton class. Specifically, only that particular class in that particular package can create the single instance of that singleton class. If one instance already exists, the singleton class won’t allow the creation of another instance. Other classes within and outside the package that contains the singleton class can obtain that instance and its properties. These same different classes can also manipulate or alter the properties of the singleton instance.

A classic example of a singleton pattern in Java.

A classic example of a singleton pattern in Java.

In the above code, the constructor of the class is private. This means that only the singleton class in the particular package that contains the class can invoke the constructor. A constructor in object-oriented programming dynamically allocates memory space for an “object” or this class instance and creates an instance of the class. This class instance is stored within the memory that was dynamically allocated for the class instance. The one instance of the class is private and also static meaning that only this singleton class has direct access to the singleton instance.

Also when a property is designated as static it describes that class. In other words, all instances of that class have those properties. Other classes in the same package and outside the same package as the singleton class have access to that instance. To ensure that only one thread at a time is executing the getter function getInstance the synchronized access modifier is added.

To learn more about the Singleton pattern I recommend reading Head First: Design Patterns by Eric Freeman and Elisabeth Robson. Another good resource is the GeekForGeeks article on the Singleton design pattern here. To view more of our blogs visit the blog section of our website here.

 

Related Posts

A Comprehensive Guide to Flexbox

A Comprehensive Guide to Flexbox

Background Before diving into the details of Flexbox, let's understand its background and purpose. The Flexbox Layout module, also known as Flexible Box, is a W3C Candidate Recommendation. It aims to provide a flexible way to lay out, align, and distribute space among...

The evolution of the web: A Journey from Web 1.0 to Web 3.0

The evolution of the web: A Journey from Web 1.0 to Web 3.0

In the ever-changing landscape of the internet, the terms “Web 1.0,” “Web 2.0,” and “Web 3.0” are frequently used to describe different eras of online development. However, these terms are often misunderstood and used interchangeably. In this article, we will delve...

What is a Website?

What is a Website?

In today's digital age, a website plays a crucial role in establishing an online presence for individuals, businesses, and organizations. It serves as a platform to share information, promote products or services, connect with an audience, and facilitate various...

Call Now Button