Singleton pattern
The Singleton pattern restricts a type or component to one shared instance within a defined scope and provides a way to access it.
ASP.NET Core in Action, 3e
A singleton pattern is a creational design pattern that ensures a service or object has one shared instance for the relevant application lifetime.
- How it is used
- Uses singletons to share one DI-managed service instance across HTTP requests.
- How deep it goes
- Explains registration, creation, reuse, thread safety, and captive dependencies; does not implement the pattern without DI.
- Examples
- Compares row counts read directly and through a repository, and registers shared email settings.
- What you can do after
- Apply it via ASP.NET Core's AddSingleton while checking thread safety and dependency lifetimes.
Read how ASP.NET Core in Action, 3e explains Singleton pattern →
Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform
A singleton pattern is a design pattern in which a resource or set of resources has one authoritative instance within a system.
- How it is used
- Deploys unique, environment-specific infrastructure resources together when one authoritative configuration is sufficient.
- How deep it goes
- Defines the pattern, demonstrates a GCP configuration, and defers detailed modular decomposition to factory, builder, and composite patterns.
- Examples
- Uses a GCP project, database network, MySQL server template, three-server group, and a testing workflow for an application server.
- What you can do after
- Operate/configure a small set of unique infrastructure resources from one static configuration and one deployment command.
Machine Learning Platform Engineering
The singleton pattern restricts a component to one shared instance and provides a common access point to it.
- How it is used
- Reuses an initialized RAG core across synchronous test queries to avoid repeated embedding-model and vector-index setup.
- How deep it goes
- Shows a module-level cached instance, an async initialization getter, and a synchronous query wrapper; does not elaborate broader singleton variants.
- Examples
- Uses a Python RAG assistant with a cached core instance and an asyncio.run() query wrapper.
- What you can do after
- Build it from scratch as a cached async getter paired with a synchronous testing interface.
Read how Machine Learning Platform Engineering explains Singleton pattern →
Software Design for Python Programmers
The Singleton Design Pattern is a creational design pattern that restricts a class to at most one object instance and provides access to that instance.
- How it is used
- Models application architecture that must provide one shared object, such as a printer spooler or executive pass.
- How deep it goes
- Explains lazy creation, access control, copy prevention, deletion, and multithreaded race conditions through Python examples.
- Examples
- Uses PrintSpooler, ExecutivePass, global state, copying, explicit construction, deletion, and reference assignment.
- What you can do after
- Build it from scratch by controlling construction, access, copying, and replacement in a Python singleton class.
Read how Software Design for Python Programmers explains Singleton pattern →
Object-Oriented Software Design in C++
The Singleton design pattern is a creational design pattern that restricts a class to one instance and provides a controlled access point to it.
- How it is used
- Uses a controlled access point when an application must manage at most one shared object.
- How deep it goes
- Explains construction, copying, lazy creation, deletion, and multithreading risks, but defers race-condition solutions.
- Examples
- Demonstrates printer spooling, a stadium executive pass, global copies, and repeated obtain-delete-obtain calls.
- What you can do after
- Build a C++ singleton with private construction, disabled copying, lazy creation, static access, and deletion-aware reuse.
Read how Object-Oriented Software Design in C++ explains Singleton pattern →
AI-Powered Developer: Build software with ChatGPT and Copilot
The Singleton Pattern is a software design pattern that restricts a class to one instance and provides a global access point to it.
- How it is used
- Coordinates one shared database connection while supporting application-wide access and replaceable persistence components.
- How deep it goes
- Defines the pattern and implements instance protection, connection creation, session generation, and application wiring.
- Examples
- Uses a DatabaseConnection class with PostgreSQL settings, SQLAlchemy sessions, and an application entry point that selects a repository.
- What you can do after
- Operate/configure a shared SQLAlchemy database connection using environment variables and integrate it with a repository-based application.
Read how AI-Powered Developer: Build software with ChatGPT and Copilot explains Singleton pattern →
Related concepts
- design patternkind of · A design pattern is a reusable, general solution structure for a recurring software design problem in a particular context.
- static fielduses · A named variable whose storage is associated with a class or has static duration, rather than being allocated separately for each object instance or function call.
- lazy initializationuses · Lazy initialization is a technique that postpones creating, retrieving, computing, or loading a value or resource until it is needed.
- AddSingletonconfigured by · AddSingleton is an ASP.NET Core dependency-injection registration method that reuses one service instance for the application's lifetime.
- global variablecontrasts with · A global variable is a variable whose binding is accessible from broad program or module scope according to the language’s name-resolution rules.
- object declarationproduced by · An object declaration is a Kotlin declaration that defines a singleton object and creates its sole instance as part of the declaration.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.





