Sync Singleton to Java 25: split 6 implementations into separate files, consolidated Main.java demo
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
package singleton;
|
|
||||||
public class AppConfig {
|
|
||||||
|
|
||||||
private AppConfig() {
|
|
||||||
System.out.println("Reading configuration from file...");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Holder {
|
|
||||||
static final AppConfig INSTANCE = new AppConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static AppConfig getInstance() {
|
|
||||||
return Holder.INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getProperty(String key) {
|
|
||||||
return System.getProperty(key, "(not set)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
31
01-creational/singleton/AppConfigDCL.java
Normal file
31
01-creational/singleton/AppConfigDCL.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package singleton;
|
||||||
|
|
||||||
|
public class AppConfigDCL {
|
||||||
|
|
||||||
|
// volatile is MANDATORY for DCL to work correctly.
|
||||||
|
// Without it, the JVM can reorder the writes inside the constructor
|
||||||
|
// and return a partially-constructed object to a second thread.
|
||||||
|
private static volatile AppConfigDCL instance;
|
||||||
|
|
||||||
|
private AppConfigDCL() { /* load config */ }
|
||||||
|
|
||||||
|
public static AppConfigDCL getInstance() {
|
||||||
|
|
||||||
|
// First check: no lock, instant return for the 99.99% case
|
||||||
|
// (instance already created, just return it)
|
||||||
|
if (instance == null) {
|
||||||
|
|
||||||
|
// Lock only when first check says we might need to create
|
||||||
|
synchronized (AppConfigDCL.class) {
|
||||||
|
|
||||||
|
// Second check: re-verify under the lock.
|
||||||
|
// Another thread might have created the instance between
|
||||||
|
// our first check and acquiring the lock.
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new AppConfigDCL();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
24
01-creational/singleton/AppConfigEager.java
Normal file
24
01-creational/singleton/AppConfigEager.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package singleton;
|
||||||
|
|
||||||
|
public class AppConfigEager {
|
||||||
|
|
||||||
|
// The JVM creates this instance when the class is first loaded.
|
||||||
|
// static guarantees it belongs to the class, not any instance.
|
||||||
|
// final guarantees it can never be reassigned.
|
||||||
|
private static final AppConfigEager INSTANCE = new AppConfigEager();
|
||||||
|
|
||||||
|
// Private constructor: prevents any other class from calling new AppConfigEager()
|
||||||
|
private AppConfigEager() {
|
||||||
|
System.out.println("Reading configuration from file...");
|
||||||
|
// load properties, set up values, etc.
|
||||||
|
}
|
||||||
|
|
||||||
|
// The only way to get the instance
|
||||||
|
public static AppConfigEager getInstance() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProperty(String key) {
|
||||||
|
return System.getProperty(key, "(not set)");
|
||||||
|
}
|
||||||
|
}
|
||||||
19
01-creational/singleton/AppConfigEnum.java
Normal file
19
01-creational/singleton/AppConfigEnum.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package singleton;
|
||||||
|
|
||||||
|
public enum AppConfigEnum {
|
||||||
|
|
||||||
|
INSTANCE; // The JVM creates this exactly once, period.
|
||||||
|
|
||||||
|
// Enum instances can have fields and methods like any class
|
||||||
|
private final String configPath;
|
||||||
|
|
||||||
|
AppConfigEnum() {
|
||||||
|
// Enum constructors run once, when the constant is first used
|
||||||
|
this.configPath = System.getProperty("config.path", "application.properties");
|
||||||
|
System.out.println("Loading config from: " + configPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProperty(String key) {
|
||||||
|
return System.getProperty(key, "(not set)");
|
||||||
|
}
|
||||||
|
}
|
||||||
19
01-creational/singleton/AppConfigHolder.java
Normal file
19
01-creational/singleton/AppConfigHolder.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package singleton;
|
||||||
|
|
||||||
|
public class AppConfigHolder {
|
||||||
|
|
||||||
|
private AppConfigHolder() { /* load config */ }
|
||||||
|
|
||||||
|
// The JVM loads Holder only when getInstance() is first called.
|
||||||
|
// Class loading is inherently thread-safe — the JVM ensures it happens once.
|
||||||
|
// No synchronized, no volatile, no locks — just the JVM class loading guarantee.
|
||||||
|
private static class Holder {
|
||||||
|
static final AppConfigHolder INSTANCE = new AppConfigHolder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AppConfigHolder getInstance() {
|
||||||
|
// This reference triggers Holder to load (if it hasn't already).
|
||||||
|
// After first call, Holder is already loaded and this is just a field read.
|
||||||
|
return Holder.INSTANCE;
|
||||||
|
}
|
||||||
|
}
|
||||||
16
01-creational/singleton/AppConfigNaiveLazy.java
Normal file
16
01-creational/singleton/AppConfigNaiveLazy.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package singleton;
|
||||||
|
|
||||||
|
public class AppConfigNaiveLazy {
|
||||||
|
|
||||||
|
private static AppConfigNaiveLazy instance; // null until first request
|
||||||
|
|
||||||
|
private AppConfigNaiveLazy() { /* load config */ }
|
||||||
|
|
||||||
|
// BROKEN IN MULTITHREADED CODE — see explanation below
|
||||||
|
public static AppConfigNaiveLazy getInstance() {
|
||||||
|
if (instance == null) { // Thread A: checks, sees null
|
||||||
|
instance = new AppConfigNaiveLazy(); // Thread B: also sees null, also enters here
|
||||||
|
} // Both threads create a new AppConfigNaiveLazy!
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
16
01-creational/singleton/AppConfigSynchronized.java
Normal file
16
01-creational/singleton/AppConfigSynchronized.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package singleton;
|
||||||
|
|
||||||
|
public class AppConfigSynchronized {
|
||||||
|
|
||||||
|
private static AppConfigSynchronized instance;
|
||||||
|
|
||||||
|
private AppConfigSynchronized() { /* load config */ }
|
||||||
|
|
||||||
|
// synchronized: only one thread can execute this method at a time
|
||||||
|
public static synchronized AppConfigSynchronized getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new AppConfigSynchronized(); // safe — no other thread can be here
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,36 @@
|
|||||||
package singleton;
|
package singleton;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AppConfig config1 = AppConfig.getInstance();
|
|
||||||
AppConfig config2 = AppConfig.getInstance();
|
System.out.println("--- Implementation 1: Eager ---");
|
||||||
System.out.println("Same instance? " + (config1 == config2)); // true
|
AppConfigEager eager1 = AppConfigEager.getInstance();
|
||||||
System.out.println("db.url = " + config1.getProperty("db.url"));
|
AppConfigEager eager2 = AppConfigEager.getInstance();
|
||||||
|
System.out.println("Same instance? " + (eager1 == eager2));
|
||||||
|
System.out.println("db.url = " + eager1.getProperty("db.url"));
|
||||||
|
|
||||||
|
System.out.println("\n--- Implementation 2: Naive Lazy ---");
|
||||||
|
AppConfigNaiveLazy lazy1 = AppConfigNaiveLazy.getInstance();
|
||||||
|
AppConfigNaiveLazy lazy2 = AppConfigNaiveLazy.getInstance();
|
||||||
|
System.out.println("Same instance? " + (lazy1 == lazy2));
|
||||||
|
|
||||||
|
System.out.println("\n--- Implementation 3: Synchronized Method ---");
|
||||||
|
AppConfigSynchronized sync1 = AppConfigSynchronized.getInstance();
|
||||||
|
AppConfigSynchronized sync2 = AppConfigSynchronized.getInstance();
|
||||||
|
System.out.println("Same instance? " + (sync1 == sync2));
|
||||||
|
|
||||||
|
System.out.println("\n--- Implementation 4: Double-Checked Locking ---");
|
||||||
|
AppConfigDCL dcl1 = AppConfigDCL.getInstance();
|
||||||
|
AppConfigDCL dcl2 = AppConfigDCL.getInstance();
|
||||||
|
System.out.println("Same instance? " + (dcl1 == dcl2));
|
||||||
|
|
||||||
|
System.out.println("\n--- Implementation 5: Holder Idiom ---");
|
||||||
|
AppConfigHolder holder1 = AppConfigHolder.getInstance();
|
||||||
|
AppConfigHolder holder2 = AppConfigHolder.getInstance();
|
||||||
|
System.out.println("Same instance? " + (holder1 == holder2));
|
||||||
|
|
||||||
|
System.out.println("\n--- Implementation 6: Enum ---");
|
||||||
|
AppConfigEnum enumConfig = AppConfigEnum.INSTANCE;
|
||||||
|
System.out.println("db.url = " + enumConfig.getProperty("db.url"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
README.md
20
README.md
@@ -158,6 +158,26 @@ javac 01-creational/prototype/*.java -d out/prototype
|
|||||||
java -cp out/prototype prototype.Main
|
java -cp out/prototype prototype.Main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Singleton (`01-creational/singleton/`)
|
||||||
|
|
||||||
|
| Post Section | File(s) |
|
||||||
|
|---|---|
|
||||||
|
| Implementation 1 — Eager Initialization | `AppConfigEager.java` |
|
||||||
|
| Implementation 2 — Naïve Lazy Initialization | `AppConfigNaiveLazy.java` |
|
||||||
|
| Implementation 3 — Synchronized Method | `AppConfigSynchronized.java` |
|
||||||
|
| Implementation 4 — Double-Checked Locking | `AppConfigDCL.java` |
|
||||||
|
| Implementation 5 — Initialization-on-Demand Holder | `AppConfigHolder.java` |
|
||||||
|
| Implementation 6 — Enum Singleton | `AppConfigEnum.java` |
|
||||||
|
| Running All Six Implementations Together | `Main.java` |
|
||||||
|
|
||||||
|
Each implementation is renamed to its own class (e.g. `AppConfigEager`, `AppConfigHolder`) so all six can be compiled together in one package and compared directly. `Main.java` exercises all six in one run. The "Breaking via Reflection," "Breaking via Serialization," and Spring `@Component` snippets are illustrative only — they are not part of this repository's runnable example.
|
||||||
|
|
||||||
|
Run it:
|
||||||
|
```bash
|
||||||
|
javac 01-creational/singleton/*.java -d out/singleton
|
||||||
|
java -cp out/singleton singleton.Main
|
||||||
|
```
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
|
||||||
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
- *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides
|
||||||
|
|||||||
Reference in New Issue
Block a user