Skip to content

Commit 04c8e35

Browse files
committed
feat: Implement Singleton design pattern with PrivateConstructor class
WHAT the code does: - Defines a `PrivateConstructor` class that restricts object creation. - Declares a static field `School` to hold the single instance. - Makes the constructor `private` so no external code can instantiate the class. - Provides `getInstance()` method: - If `School` is `null`, creates a new instance. - Otherwise, returns the existing instance. - `main()` repeatedly calls `getInstance()` to demonstrate that only one object is created. WHY this matters: - Implements the **Singleton pattern** — ensures only one instance of a class exists. - Useful for cases like configuration managers, logging, thread pools, or database connections. - Demonstrates **controlled object creation** using private constructors and static methods. HOW it works: 1. First call to `getInstance()` → `School` is null → creates new `PrivateConstructor` object. 2. Later calls return the same object reference, preventing multiple creations. 3. Program ensures all callers use the same singleton instance. Tips & gotchas: - This is a **lazy initialization** singleton → instance created only when needed. - Not thread-safe: multiple threads could create multiple instances at the same time. → Can fix with synchronized `getInstance()` or double-checked locking. - For strict immutability, mark the singleton instance as `final` and initialize eagerly. - Enum-based singletons in Java are simpler and thread-safe by default. Use-cases: - Logger classes. - Database connection pools. - Application configuration objects. - Resource managers that must be globally consistent. Short key: pattern-singleton-private constructor. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 2334fae commit 04c8e35

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
public class PrivateConstructor
2-
{
3-
//Our aim is to create only one object of our class Private Constructor
4-
5-
private static PrivateConstructor School; //Vaiable leya hai
6-
//Instance create keya hai.
7-
1+
public class PrivateConstructor {
2+
// Our aim is to create only one object of our class Private Constructor
3+
private static PrivateConstructor School;
4+
/*
5+
Variable leya hai.
6+
Instance create keya hai.
7+
*/
88

99
//Private constructor create keya hai toh object create kar nahi sakte.
10-
private PrivateConstructor(){
10+
private PrivateConstructor() {
1111

1212
}
1313

14-
//Method....Instance return karega
15-
public static PrivateConstructor getInstance()
16-
{
17-
if(School == null)
18-
{
14+
// Method....Instance return karega
15+
public static PrivateConstructor getInstance() {
16+
if (School == null) {
1917
School = new PrivateConstructor();
2018
}
2119
return School;
2220
}
23-
2421
public static void main(String[] args) {
2522
PrivateConstructor.getInstance();
2623
PrivateConstructor.getInstance();
@@ -29,7 +26,9 @@ public static void main(String[] args) {
2926
PrivateConstructor.getInstance();
3027
PrivateConstructor.getInstance();
3128
PrivateConstructor.getInstance();
32-
//Isko Bolte hai SingleTone Pattern, Ek hai object Hoga create Ek Class ka
33-
//Ye Hamne Restrication Rakh Deya hai
29+
/*
30+
Isko Bolte hai SingleTone Pattern, Ek hai object Hoga create Ek Class ka
31+
Ye Hamne Restrication Rakh Deya hai
32+
*/
3433
}
35-
}
34+
}

0 commit comments

Comments
 (0)