DELAPARRA
Index
Color
NL
Category: Design patterns

Singleton

Explained by Updated

What is Singleton?

Singleton: Ensure only one instance of a class exists, and provide a global access point to it. The Singleton pattern ensures a class has only one instance and gives global access to it. A configuration object or a database connection pool are classic examples. It is easy to implement and easy to overuse. A singleton is global state in disguise: any code can reach it and change it, which hides dependencies and makes testing harder, since tests can affect each other through the shared instance.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Singleton

What should you keep in mind with Singleton?

Modern frameworks offer a better way. In Laravel, you register a class as a singleton in the service container and receive it through dependency injection. You still get one shared instance, but dependencies are explicit and tests can swap in a fake. In long-running setups such as Octane, be careful that singletons do not carry data from one request to the next.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with S

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.