Commit c957bcb
committed
feat: Implement Singleton design pattern with CoffeeMachine class
WHAT the code does:
- Defines CoffeeMachine with private fields for coffee, milk, water, and sugar quantities.
- Uses a **private constructor** to prevent external instantiation.
- Maintains a static private reference `my` to hold the single instance.
- Provides a static factory method getInstance():
- Creates a new CoffeeMachine only if none exists.
- Returns the same object reference for all calls.
- Adds utility methods:
- fillWater() and fillSugar() to adjust quantities.
- getCoffee() returning a fixed coffee amount (0.15f).
- SingletoneClass main():
- Calls CoffeeMachine.getInstance() three times into m1, m2, m3.
- Prints references, confirming they point to the same object.
- Verifies identity with `==` operator, printing "Same".
WHY this matters:
- Demonstrates the **Singleton Pattern**, ensuring only one instance of a class exists across the JVM.
- Useful when:
- A single shared resource is needed (e.g., database connection, logging system, configuration manager).
- Object creation is costly, and reusing one instance is efficient.
- Shows how private constructors and static methods enforce singleton behavior.
HOW it works:
1. First call to getInstance() → my is null, new CoffeeMachine is created and stored in my.
2. Subsequent calls → my already holds the instance, so the same object is returned.
3. Printing references proves all variables point to the same memory location.
4. Identity check (==) confirms all references refer to the same singleton object.
Tips and gotchas:
- This implementation is **not thread-safe**. Multiple threads calling getInstance() simultaneously could create multiple objects.
- Fix: synchronize getInstance() or use double-checked locking with volatile, or enum-based singleton.
- Singleton can make testing harder if not carefully managed; consider dependency injection for flexibility.
- Class names should follow conventions: SingletoneClass → SingletonClass, coffeQty → coffeeQty.
- Returning a fixed getCoffee() value is just illustrative; real logic could reduce coffeeQty, waterQty, etc.
Use-cases / analogies:
- Coffee machine analogy: only one machine available in a kitchen, everyone uses the same one.
- Logging: one logger instance shared across application modules.
- Configuration: one global configuration object accessed everywhere.
Short key: java-oop singleton-pattern private-constructor static-factory shared-instance.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f0161f8 commit c957bcb
1 file changed
+10
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
12 | | - | |
13 | | - | |
| 9 | + | |
14 | 10 | | |
15 | 11 | | |
16 | 12 | | |
17 | 13 | | |
18 | 14 | | |
19 | 15 | | |
20 | | - | |
21 | | - | |
| 16 | + | |
22 | 17 | | |
23 | 18 | | |
24 | | - | |
25 | | - | |
| 19 | + | |
| 20 | + | |
26 | 21 | | |
27 | 22 | | |
28 | | - | |
29 | | - | |
| 23 | + | |
| 24 | + | |
30 | 25 | | |
31 | 26 | | |
32 | | - | |
33 | | - | |
| 27 | + | |
| 28 | + | |
34 | 29 | | |
35 | 30 | | |
36 | 31 | | |
37 | 32 | | |
38 | 33 | | |
| 34 | + | |
39 | 35 | | |
40 | 36 | | |
41 | 37 | | |
| |||
45 | 41 | | |
46 | 42 | | |
47 | 43 | | |
48 | | - | |
49 | 44 | | |
50 | 45 | | |
0 commit comments