Skip to content

Commit c957bcb

Browse files
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

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
//Pending solve it onces again.
2-
3-
class CoffeeMachine
4-
{
1+
class CoffeeMachine {
52
private float coffeQty;
63
private float milkQty;
74
private float waterQty;
85
private float sugarQty;
96

107
static private CoffeeMachine my=null;
118

12-
private CoffeeMachine()
13-
{
9+
private CoffeeMachine() {
1410
coffeQty=1;
1511
milkQty=1;
1612
waterQty=1;
1713
sugarQty=1;
1814
}
1915

20-
public void fillWater(float qty)
21-
{
16+
public void fillWater(float qty) {
2217
waterQty=qty;
2318
}
24-
public void fillSugar(float qty)
25-
{
19+
20+
public void fillSugar(float qty) {
2621
sugarQty=qty;
2722
}
28-
public float getCoffee()
29-
{
23+
24+
public float getCoffee() {
3025
return 0.15f;
3126
}
32-
static CoffeeMachine getInstance()
33-
{
27+
28+
static CoffeeMachine getInstance() {
3429
if(my==null)
3530
my=new CoffeeMachine();
3631
return my;
3732
}
3833
}
34+
3935
public class SingletoneClass {
4036
public static void main(String[] args) {
4137
CoffeeMachine m1=CoffeeMachine.getInstance();
@@ -45,6 +41,5 @@ public static void main(String[] args) {
4541
System.out.println(m1+" "+m2+" "+m3);
4642
if(m1==m2 && m1==m3)
4743
System.out.println("Same");
48-
4944
}
5045
}

0 commit comments

Comments
 (0)