#590 Add new presentation for Singleton

This commit is contained in:
Ilkka Seppälä 2017-08-12 18:20:45 +03:00
parent 600553ec0f
commit 746e452c2b
5 changed files with 27 additions and 149 deletions

View File

@ -15,7 +15,32 @@ tags:
Ensure a class only has one instance, and provide a global point of
access to it.
![alt text](./etc/singleton_1.png "Singleton")
## Explanation
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.
In plain words
> Ensures that only one object of a particular class is ever created.
Wikipedia says
> 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.
**Programmatic Example**
Joshua Bloch, Effective Java 2nd Edition p.18
> A single-element enum type is the best way to implement a singleton
```
public enum EnumIvoryTower {
INSTANCE;
}
```
Then in order to use
```
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
assertEquals(enumIvoryTower1, enumIvoryTower2); // true
```
## Applicability
Use the Singleton pattern when
@ -40,7 +65,7 @@ Use the Singleton pattern when
* 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.
* Creates tightly coupled code that is difficult to test.
* Creates tightly coupled code. The clients of the Singleton become difficult to test.
* Makes it almost impossible to subclass a Singleton.
## Credits

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

View File

@ -1,104 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.singleton.InitializingOnDemandHolderIdiom.HelperHolder"
project="singleton" file="/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java"
binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="133" x="328" y="457"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.singleton.IvoryTower" project="singleton"
file="/singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="164" x="114" y="275"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.singleton.InitializingOnDemandHolderIdiom" project="singleton"
file="/singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="142" width="295" x="328" y="275"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<enumeration id="4" language="java" name="com.iluwatar.singleton.EnumIvoryTower" project="singleton"
file="/singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java" binary="false" corner="BOTTOM_RIGHT">
<position height="124" width="190" x="663" y="275"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</enumeration>
<class id="5" language="java" name="com.iluwatar.singleton.ThreadSafeLazyLoadedIvoryTower" project="singleton"
file="/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="106" width="299" x="893" y="275"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="6" language="java" name="com.iluwatar.singleton.ThreadSafeDoubleCheckLocking" project="singleton"
file="/singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="106" width="292" x="1242" y="275"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<association id="7">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="8" name="instance"/>
<multiplicity id="9" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="10">
<end type="SOURCE" refId="6" navigable="false">
<attribute id="11" name="INSTANCE"/>
<multiplicity id="12" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="6" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="13">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="14" name="INSTANCE"/>
<multiplicity id="15" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="3" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<nesting id="16">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="1"/>
</nesting>
<association id="17">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="18" name="instance"/>
<multiplicity id="19" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="5" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@ -1,43 +0,0 @@
@startuml
package com.iluwatar.singleton {
class App {
- LOGGER : Logger {static}
+ App()
+ main(args : String[]) {static}
}
enum EnumIvoryTower {
+ INSTANCE {static}
+ toString() : String
+ valueOf(name : String) : EnumIvoryTower {static}
+ values() : EnumIvoryTower[] {static}
}
class InitializingOnDemandHolderIdiom {
- InitializingOnDemandHolderIdiom()
+ getInstance() : InitializingOnDemandHolderIdiom {static}
}
-class HelperHolder {
- INSTANCE : InitializingOnDemandHolderIdiom {static}
- HelperHolder()
}
class IvoryTower {
- INSTANCE : IvoryTower {static}
- IvoryTower()
+ getInstance() : IvoryTower {static}
}
class ThreadSafeDoubleCheckLocking {
- instance : ThreadSafeDoubleCheckLocking {static}
- ThreadSafeDoubleCheckLocking()
+ getInstance() : ThreadSafeDoubleCheckLocking {static}
}
class ThreadSafeLazyLoadedIvoryTower {
- instance : ThreadSafeLazyLoadedIvoryTower {static}
- ThreadSafeLazyLoadedIvoryTower()
+ getInstance() : ThreadSafeLazyLoadedIvoryTower {static}
}
}
IvoryTower --> "-INSTANCE" IvoryTower
ThreadSafeDoubleCheckLocking --> "-instance" ThreadSafeDoubleCheckLocking
ThreadSafeLazyLoadedIvoryTower --> "-instance" ThreadSafeLazyLoadedIvoryTower
HelperHolder ..+ InitializingOnDemandHolderIdiom
HelperHolder --> "-INSTANCE" InitializingOnDemandHolderIdiom
@enduml

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB