Skip to content

Commit 7343677

Browse files
committed
Step 2: applying singleton pattern
1 parent c0a123c commit 7343677

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

pattern/src/com/premaseem/Client.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
public class Client {
1010
public static void main (String[] args) {
1111
System.out.println("Singleton cook example ");
12-
Cook cook1 = new Cook();
13-
Cook cook2 = new Cook();
14-
Cook cook3 = new Cook();
12+
Cook cook1 = Cook.getInstance();
13+
Cook cook2 = Cook.getInstance();
14+
Cook cook3 = Cook.getInstance();
1515

1616
cook1.makeBroth();
1717
cook2.makeBroth();

pattern/src/com/premaseem/Cook.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@
88
*/
99
public class Cook {
1010

11-
public Cook(){
11+
private Cook(){
1212

1313
}
1414

15+
private static Cook uniqueInstance = null;
16+
17+
public static Cook getInstance(){
18+
19+
if(uniqueInstance == null){
20+
uniqueInstance = new Cook();
21+
}
22+
return uniqueInstance;
23+
}
1524
boolean saltAdded = false;
1625

1726
public void makeBroth(){

0 commit comments

Comments
 (0)