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

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...

What is Mixed Reality: Exploring the Future of Technology

What is Mixed Reality: Exploring the Future of Technology

Introduction In a world where technology continues to evolve at an astonishing pace, the line between the virtual and real world is becoming increasingly blurred. What was once only imaginable in the realm of science fiction is now becoming a breathtaking reality....

The Languages of the Web: HTML, CSS, and JavaScript

The Languages of the Web: HTML, CSS, and JavaScript

In the world of web development, three languages reign supreme: HTML, CSS, and JavaScript. These languages are the building blocks of the web, allowing developers to create dynamic and interactive websites. In this article, we will explore the basics of HTML, CSS, and...

Call Now Button