Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update singleton
  • Loading branch information
iluwatar committed Jun 22, 2021
commit 9a2c5aa9aaf355b8e22cfc0b7392283d153f780a
27 changes: 17 additions & 10 deletions singleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ tags:

Ensure a class only has one instance, and provide a global point of access to it.


## Explanation

Real world example
Real-world example

> There can only be one ivory tower where the wizards study their magic. The same enchanted ivory
> tower is always used by the wizards. Ivory tower here is singleton.
> tower is always used by the wizards. The ivory tower here is a singleton.

In plain words

Expand All @@ -46,9 +45,17 @@ public enum EnumIvoryTower {
Then in order to use:

```java
var enumIvoryTower1 = EnumIvoryTower.INSTANCE;
var enumIvoryTower2 = EnumIvoryTower.INSTANCE;
assertEquals(enumIvoryTower1, enumIvoryTower2); // true
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
```

## Class diagram
Expand All @@ -62,13 +69,13 @@ Use the Singleton pattern when
* 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

## Typical Use Case
Some typical use cases for the Singleton

* The logging class
* Managing a connection to a database
* File manager

## Real world examples
## Known uses

* [java.lang.Runtime#getRuntime()](http://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#getRuntime%28%29)
* [java.awt.Desktop#getDesktop()](http://docs.oracle.com/javase/8/docs/api/java/awt/Desktop.html#getDesktop--)
Expand All @@ -77,8 +84,8 @@ Use the Singleton pattern when

## Consequences

* Violates Single Responsibility Principle (SRP) by controlling their own creation and lifecycle.
* Encourages using a global shared instance which prevents an object and resources used by this object from being deallocated.
* 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.
* Creates tightly coupled code. The clients of the Singleton become difficult to test.
* Makes it almost impossible to subclass a Singleton.

Expand Down
16 changes: 8 additions & 8 deletions singleton/src/main/java/com/iluwatar/singleton/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
* classloader instance and provides global access to it.</p>
*
* <p>One of the risks of this pattern is that bugs resulting from setting a singleton up in a
* distributed environment can be tricky to debug, since it will work fine if you debug with a
* distributed environment can be tricky to debug since it will work fine if you debug with a
* single classloader. Additionally, these problems can crop up a while after the implementation of
* a singleton, since they may start out synchronous and only become async with time, so it may
* not be clear why you are seeing certain changes in behaviour.</p>
* a singleton, since they may start synchronous and only become async with time, so it may
* not be clear why you are seeing certain changes in behavior.</p>
*
* <p>There are many ways to implement the Singleton. The first one is the eagerly initialized
* instance in {@link IvoryTower}. Eager initialization implies that the implementation is thread
* safe. If you can afford giving up control of the instantiation moment, then this implementation
* safe. If you can afford to give up control of the instantiation moment, then this implementation
* will suit you fine.</p>
*
* <p>The other option to implement eagerly initialized Singleton is enum based Singleton. The
* example is found in {@link EnumIvoryTower}. At first glance the code looks short and simple.
* <p>The other option to implement eagerly initialized Singleton is enum-based Singleton. The
* example is found in {@link EnumIvoryTower}. At first glance, the code looks short and simple.
* However, you should be aware of the downsides including committing to implementation strategy,
* extending the enum class, serializability and restrictions to coding. These are extensively
* extending the enum class, serializability, and restrictions to coding. These are extensively
* discussed in Stack Overflow: http://programmers.stackexchange.com/questions/179386/what-are-the-downsides-of-implementing
* -a-singleton-with-javas-enum</p>
*
Expand All @@ -56,7 +56,7 @@
* ThreadSafeLazyLoadedIvoryTower} since it doesn't synchronize the whole access method but only the
* method internals on specific conditions.</p>
*
* <p>Yet another way to implement thread safe lazily initialized Singleton can be found in
* <p>Yet another way to implement thread-safe lazily initialized Singleton can be found in
* {@link InitializingOnDemandHolderIdiom}. However, this implementation requires at least Java 8
* API level to work.</p>
*/
Expand Down