2015-08-13 23:54:40 +02:00
---
layout: pattern
title: Singleton
folder: singleton
2015-08-15 18:03:05 +02:00
permalink: /patterns/singleton/
2015-08-20 21:40:07 +02:00
categories: Creational
2021-05-19 10:49:05 -06:00
language: en
2015-09-22 18:25:56 +05:30
tags:
2019-12-13 22:22:11 +02:00
- Gang of Four
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Intent
2020-09-13 17:53:50 +03:00
Ensure a class only has one instance, and provide a global point of access to it.
2015-08-13 23:54:40 +02:00
2017-08-12 18:20:45 +03:00
## Explanation
2020-09-13 17:53:50 +03:00
2021-06-24 15:57:20 +03:00
Real-world example
2017-08-12 21:44:21 +03:00
2020-09-13 17:53:50 +03:00
> There can only be one ivory tower where the wizards study their magic. The same enchanted ivory
2021-06-24 15:57:20 +03:00
> tower is always used by the wizards. The ivory tower here is a singleton.
2017-08-12 18:20:45 +03:00
In plain words
2017-08-12 21:44:21 +03:00
2017-08-12 18:20:45 +03:00
> Ensures that only one object of a particular class is ever created.
Wikipedia says
2017-08-12 21:44:21 +03:00
2020-09-13 17:53:50 +03:00
> In software engineering, the singleton pattern is a software design pattern that restricts the
> instantiation of a class to one object. This is useful when exactly one object is needed to
> coordinate actions across the system.
2017-08-12 18:20:45 +03:00
**Programmatic Example**
Joshua Bloch, Effective Java 2nd Edition p.18
2017-08-12 21:44:21 +03:00
2017-08-12 18:20:45 +03:00
> A single-element enum type is the best way to implement a singleton
2017-08-12 21:44:21 +03:00
2018-03-28 01:35:43 -04:00
```java
2017-08-12 18:20:45 +03:00
public enum EnumIvoryTower {
2020-07-30 20:28:47 +03:00
INSTANCE
2017-08-12 18:20:45 +03:00
}
```
2017-08-12 21:44:21 +03:00
2020-09-13 17:53:50 +03:00
Then in order to use:
2017-08-12 21:44:21 +03:00
2018-03-28 01:35:43 -04:00
```java
2021-06-24 15:57:20 +03:00
var enumIvoryTower1 = EnumIvoryTower.INSTANCE;
var enumIvoryTower2 = EnumIvoryTower.INSTANCE;
LOGGER.info("enumIvoryTower1={}", enumIvoryTower1);
LOGGER.info("enumIvoryTower2={}", enumIvoryTower2);
```
The console output
```
enumIvoryTower1=com.iluwatar.singleton.EnumIvoryTower@1221555852
enumIvoryTower2=com.iluwatar.singleton.EnumIvoryTower@1221555852
2017-08-12 18:20:45 +03:00
```
2015-08-13 23:54:40 +02:00
2019-12-07 20:01:13 +02:00
## Class diagram
2020-09-13 17:53:50 +03:00
2019-12-07 20:01:13 +02:00

2016-01-03 21:14:30 +01:00
## Applicability
2020-09-13 17:53:50 +03:00
2016-01-03 21:14:30 +01:00
Use the Singleton pattern when
2015-08-13 23:54:40 +02:00
2019-12-13 21:09:28 +02:00
* There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point
* When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code
2015-08-13 23:54:40 +02:00
2021-06-24 15:57:20 +03:00
Some typical use cases for the Singleton
2015-08-13 23:54:40 +02:00
2019-12-13 21:09:28 +02:00
* The logging class
* Managing a connection to a database
* File manager
2015-08-13 23:54:40 +02:00
2021-06-24 15:57:20 +03:00
## Known uses
2015-08-13 23:54:40 +02:00
2015-08-15 18:03:05 +02:00
* [java.lang.Runtime#getRuntime() ](http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#getRuntime%28%29 )
2016-08-20 13:17:53 +05:30
* [java.awt.Desktop#getDesktop() ](http://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html#getDesktop-- )
* [java.lang.System#getSecurityManager() ](http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getSecurityManager-- )
2015-09-22 18:25:56 +05:30
2016-10-04 14:34:01 +02:00
## Consequences
2021-06-24 15:57:20 +03:00
* Violates Single Responsibility Principle (SRP) by controlling their creation and lifecycle.
* Encourages using a globally shared instance which prevents an object and resources used by this object from being deallocated.
2017-08-12 18:20:45 +03:00
* Creates tightly coupled code. The clients of the Singleton become difficult to test.
2016-10-18 14:18:47 +02:00
* Makes it almost impossible to subclass a Singleton.
2016-10-04 14:34:01 +02:00
2016-01-03 21:14:30 +01:00
## Credits
2015-09-22 18:25:56 +05:30
2020-07-06 13:31:07 +03:00
* [Design Patterns: Elements of Reusable Object-Oriented Software ](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59 )
* [Effective Java ](https://www.amazon.com/gp/product/0134685997/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134685997&linkCode=as2&tag=javadesignpat-20&linkId=4e349f4b3ff8c50123f8147c828e53eb )
2020-07-07 18:05:11 +03:00
* [Head First Design Patterns: A Brain-Friendly Guide ](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b )
2020-07-07 18:44:00 +03:00
* [Refactoring to Patterns ](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7 )