Skip to content

Commit 746e452

Browse files
committed
iluwatar#590 Add new presentation for Singleton
1 parent 600553e commit 746e452

File tree

5 files changed

+27
-149
lines changed

5 files changed

+27
-149
lines changed

singleton/README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,32 @@ tags:
1515
Ensure a class only has one instance, and provide a global point of
1616
access to it.
1717

18-
![alt text](./etc/singleton_1.png "Singleton")
18+
19+
## Explanation
20+
Real world example
21+
> 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.
22+
23+
In plain words
24+
> Ensures that only one object of a particular class is ever created.
25+
26+
Wikipedia says
27+
> 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.
28+
29+
**Programmatic Example**
30+
31+
Joshua Bloch, Effective Java 2nd Edition p.18
32+
> A single-element enum type is the best way to implement a singleton
33+
```
34+
public enum EnumIvoryTower {
35+
INSTANCE;
36+
}
37+
```
38+
Then in order to use
39+
```
40+
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
41+
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
42+
assertEquals(enumIvoryTower1, enumIvoryTower2); // true
43+
```
1944

2045
## Applicability
2146
Use the Singleton pattern when
@@ -40,7 +65,7 @@ Use the Singleton pattern when
4065

4166
* Violates Single Responsibility Principle (SRP) by controlling their own creation and lifecycle.
4267
* Encourages using a global shared instance which prevents an object and resources used by this object from being deallocated.
43-
* Creates tightly coupled code that is difficult to test.
68+
* Creates tightly coupled code. The clients of the Singleton become difficult to test.
4469
* Makes it almost impossible to subclass a Singleton.
4570

4671
## Credits

singleton/etc/singleton.png

-21.8 KB
Binary file not shown.

singleton/etc/singleton.ucls

Lines changed: 0 additions & 104 deletions
This file was deleted.

singleton/etc/singleton.urm.puml

Lines changed: 0 additions & 43 deletions
This file was deleted.

singleton/etc/singleton_1.png

-16 KB
Binary file not shown.

0 commit comments

Comments
 (0)