Skip to content

Commit 1894fcf

Browse files
feat: Add Bill Pugh Impl & Test (iluwatar#2538)
* Add Bill Pugh Impl & Test Add Bill Pugh Impl & Test * Fix formatting * Fix checkstyle error * Reformat file * Reformat file * Fix indentation * Fix comment indent
1 parent 8b11e76 commit 1894fcf

8 files changed

+140
-2
lines changed

Diff for: singleton/src/main/java/com/iluwatar/singleton/App.java

+6
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,11 @@ public static void main(String[] args) {
100100
LOGGER.info(demandHolderIdiom.toString());
101101
var demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
102102
LOGGER.info(demandHolderIdiom2.toString());
103+
104+
// initialize singleton using Bill Pugh's implementation
105+
var billPughSingleton = BillPughImplementation.getInstance();
106+
LOGGER.info(billPughSingleton.toString());
107+
var billPughSingleton2 = BillPughImplementation.getInstance();
108+
LOGGER.info(billPughSingleton2.toString());
103109
}
104110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.singleton;
26+
27+
/**
28+
* <p>Bill Pugh Singleton Implementation.</p>
29+
*
30+
* <p>This implementation of the singleton design pattern takes advantage of the
31+
* Java memory model's guarantees about class initialization. Each class is
32+
* initialized only once, when it is first used. If the class hasn't been used
33+
* yet, it won't be loaded into memory, and no memory will be allocated for
34+
* a static instance. This makes the singleton instance lazy-loaded and thread-safe.</p>
35+
*
36+
37+
*/
38+
public final class BillPughImplementation {
39+
40+
/**
41+
* Private constructor to prevent instantiation from outside the class.
42+
*/
43+
private BillPughImplementation() {
44+
// private constructor
45+
}
46+
47+
/**
48+
* The InstanceHolder is a static inner class and it holds the Singleton instance.
49+
* It is not loaded into memory until the getInstance() method is called.
50+
*/
51+
private static class InstanceHolder {
52+
/**
53+
* Singleton instance of the class.
54+
*/
55+
private static BillPughImplementation instance = new BillPughImplementation();
56+
}
57+
58+
/**
59+
* Public accessor for the singleton instance.
60+
*
61+
* <p>
62+
* When this method is called, the InstanceHolder is loaded into memory
63+
* and creates the Singleton instance. This method provides a global access point
64+
* for the singleton instance.
65+
* </p>
66+
*
67+
* @return an instance of the class.
68+
*/
69+
// global access point
70+
public static BillPughImplementation getInstance() {
71+
return InstanceHolder.instance;
72+
}
73+
}

Diff for: singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
*/
3333
public enum EnumIvoryTower {
3434

35+
/**
36+
* The singleton instance of the class, created by the Java enum singleton pattern.
37+
*/
3538
INSTANCE;
3639

3740
@Override

Diff for: singleton/src/main/java/com/iluwatar/singleton/InitializingOnDemandHolderIdiom.java

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public static InitializingOnDemandHolderIdiom getInstance() {
5858
* Provides the lazy-loaded Singleton instance.
5959
*/
6060
private static class HelperHolder {
61+
62+
/**
63+
* Singleton instance of the class.
64+
*/
6165
private static final InitializingOnDemandHolderIdiom INSTANCE =
6266
new InitializingOnDemandHolderIdiom();
6367
}

Diff for: singleton/src/main/java/com/iluwatar/singleton/ThreadSafeDoubleCheckLocking.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
3535
*/
3636
public final class ThreadSafeDoubleCheckLocking {
37-
37+
/**
38+
* Singleton instance of the class, declared as volatile to ensure atomic access by multiple threads.
39+
*/
3840
private static volatile ThreadSafeDoubleCheckLocking instance;
3941

4042
/**
@@ -73,7 +75,8 @@ public static ThreadSafeDoubleCheckLocking getInstance() {
7375
// The instance is still not initialized so we can safely
7476
// (no other thread can enter this zone)
7577
// create an instance and make it our singleton instance.
76-
instance = result = new ThreadSafeDoubleCheckLocking();
78+
result = new ThreadSafeDoubleCheckLocking();
79+
instance = result;
7780
}
7881
}
7982
}

Diff for: singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java

+8
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
*/
3232
public final class ThreadSafeLazyLoadedIvoryTower {
3333

34+
/**
35+
* Singleton instance of the class, declared as volatile to ensure atomic access by multiple threads.
36+
*/
3437
private static volatile ThreadSafeLazyLoadedIvoryTower instance;
3538

39+
/**
40+
* Private constructor to prevent instantiation from outside the class.
41+
*/
3642
private ThreadSafeLazyLoadedIvoryTower() {
3743
// Protect against instantiation via reflection
3844
if (instance != null) {
@@ -42,6 +48,8 @@ private ThreadSafeLazyLoadedIvoryTower() {
4248

4349
/**
4450
* The instance doesn't get created until the method is called for the first time.
51+
*
52+
* @return an instance of the class.
4553
*/
4654
public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance() {
4755
if (instance == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.singleton;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.iluwatar.singleton;
2+
3+
/**
4+
* Date: 06/18/23 - 16:29 PM.
5+
*
6+
* @author Owen Leung
7+
*/
8+
public class BillPughImplementationTest
9+
extends SingletonTest<BillPughImplementation>{
10+
/**
11+
* Create a new singleton test instance using the given 'getInstance' method.
12+
*/
13+
public BillPughImplementationTest() {
14+
super(BillPughImplementation::getInstance);
15+
}
16+
}

0 commit comments

Comments
 (0)