Skip to content

Commit 9a2c5aa

Browse files
committed
Update singleton
1 parent ebcc857 commit 9a2c5aa

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

singleton/README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ tags:
1313

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

16-
1716
## Explanation
1817

19-
Real world example
18+
Real-world example
2019

2120
> There can only be one ivory tower where the wizards study their magic. The same enchanted ivory
22-
> tower is always used by the wizards. Ivory tower here is singleton.
21+
> tower is always used by the wizards. The ivory tower here is a singleton.
2322
2423
In plain words
2524

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

4847
```java
49-
var enumIvoryTower1 = EnumIvoryTower.INSTANCE;
50-
var enumIvoryTower2 = EnumIvoryTower.INSTANCE;
51-
assertEquals(enumIvoryTower1, enumIvoryTower2); // true
48+
var enumIvoryTower1 = EnumIvoryTower.INSTANCE;
49+
var enumIvoryTower2 = EnumIvoryTower.INSTANCE;
50+
LOGGER.info("enumIvoryTower1={}", enumIvoryTower1);
51+
LOGGER.info("enumIvoryTower2={}", enumIvoryTower2);
52+
```
53+
54+
The console output
55+
56+
```
57+
enumIvoryTower1=com.iluwatar.singleton.EnumIvoryTower@1221555852
58+
enumIvoryTower2=com.iluwatar.singleton.EnumIvoryTower@1221555852
5259
```
5360

5461
## Class diagram
@@ -62,13 +69,13 @@ Use the Singleton pattern when
6269
* There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point
6370
* When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code
6471

65-
## Typical Use Case
72+
Some typical use cases for the Singleton
6673

6774
* The logging class
6875
* Managing a connection to a database
6976
* File manager
7077

71-
## Real world examples
78+
## Known uses
7279

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

7885
## Consequences
7986

80-
* Violates Single Responsibility Principle (SRP) by controlling their own creation and lifecycle.
81-
* Encourages using a global shared instance which prevents an object and resources used by this object from being deallocated.
87+
* Violates Single Responsibility Principle (SRP) by controlling their creation and lifecycle.
88+
* Encourages using a globally shared instance which prevents an object and resources used by this object from being deallocated.
8289
* Creates tightly coupled code. The clients of the Singleton become difficult to test.
8390
* Makes it almost impossible to subclass a Singleton.
8491

singleton/src/main/java/com/iluwatar/singleton/App.java

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

0 commit comments

Comments
 (0)