Singleton pattern
exemplifying the singleton pattern. In object-oriented programming, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. It is one of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems in object-oriented software. The pattern is useful when exactly one object is needed to coordinate actions across a system. More specifically, the singleton pattern allows classes to: * Ensure they only have one instance * Provide easy access to that instance * Control their instantiation (for example, hiding the constructors of a class) The term comes from the mathematical concept of a singleton.
exemplifying the singleton pattern. In object-oriented programming, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. It is one of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems in object-oriented software. The pattern is useful when exactly one object is needed to coordinate actions across a system. More specifically, the singleton pattern allows classes to: Ensure they only have one instance Provide easy access to that instance Control their instantiation (for example, hiding the constructors of a class) The term comes from the mathematical concept of a singleton. import std; class Singleton { private: Singleton() = default; // no public constructor ~Singleton() = default; // no public destructor inline static Singleton* inst = nullptr; // declaration class variable int value; public: // defines a class operation that lets clients access its unique instance. static Singleton& instance() { if (!inst) { inst = new Singleton(); } return *inst; } Singleton(const Singleton&) = delete("Copy construction disabled"); Singleton& operator=(const Singleton&) = delete("Copy assignment disabled"); static void reset() { delete inst; inst = nullptr; } // existing interface goes here nodiscard int getValue() const noexcept { return value; } void setValue(int v) noexcept { value = v; } }; int main() { Singleton::instance().setValue(42); std::println("value = {}", Singleton::instance().getValue()); // prints "value = 42" Singleton::reset(); } import std; class Singleton { private: Singleton() = default; ~Singleton() = default; int value; public: static Singleton& instance() { static Singleton instance; return instance; } nodiscard int getValue() const noexcept { return value; } void setValue(int v) noexcept { value = v; } }; int main() { Singleton::instance().setValue(42); std::println("value = {}", Singleton::instance().getValue()); // prints "value = 42" } While the Meyers singleton is mostly unique to C++ due to C++'s function-local static variables with lazy thread-safe initialization, it can still be emulated in other languages. For example, in Java, one can use the initialization-on-demand holder idiom: public class Singleton { private Singleton() {} private static class Holder { static final Singleton INSTANCE = new Singleton(); } public static Singleton instance() { return Holder.INSTANCE; } } public class Singleton { private static volatile Singleton instance = null; private Singleton() {} public static Singleton instance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } In a manner similar to the Meyers singleton, one can use System.Lazy in C# to use lazy initialization: using System; public sealed class Singleton { private static readonly Lazy _instance = new(() => new Singleton()); public static Singleton Instance => _instance.Value; private Singleton() {} } // a concrete singleton class class DatabaseConnection { private static DatabaseConnection instance; private DatabaseConnection() { // database resource initialization } public static synchronized DatabaseConnection instance() { if (instance == null) { instance = new DatabaseConnection(); } return instance; } public void executeQuery(String sql) { System.out.printf("Executing: %s%n"); } } // a tightly-coupled consumer class class OrderService { public void placeOrder(String orderId) { // hardcoded dependency lookup makes this impossible to unit test DatabaseConnection db = DatabaseConnection.instance(); db.executeQuery(String.format("INSERT INTO orders VALUES ('%s')")); } } Dependency injection avoids asking how the database is created, or how many instances exist, and only asks for the Database interface in its constructor: // an abstract interface interface Database { void executeQuery(String sql); } // implement the concrete class (no singleton logic) class SqlDatabase implements Database { @Override public void executeQuery(String sql) { System.out.println("Executing SQL: %s", sql); } } // the consumer class accepts dependency via constructor injection class OrderService { private final Database db; // The dependency is injected from the outside public OrderService(Database db) { this.db = db; } public void placeOrder(String orderId) { db.executeQuery(String.format("INSERT INTO orders VALUES ('%s')", orderId)); } }
References
No reference rows published for this article.
Cite this article
MLA
"Singleton pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/singleton-pattern. Accessed 24 November 2026.
Chicago
"Singleton pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/singleton-pattern.
APA
encyclopegia.org. (2026). Singleton pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/singleton-pattern
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON