001package headfirst.singleton.chocolate; 002 003public class ChocolateBoiler { 004 private boolean empty; 005 private boolean boiled; 006 private static ChocolateBoiler uniqueInstance; 007 008 private ChocolateBoiler() { 009 empty = true; 010 boiled = false; 011 } 012 013 public static ChocolateBoiler getInstance() { 014 if (uniqueInstance == null) { 015 System.out.println("Creating unique instance of Chocolate Boiler"); 016 uniqueInstance = new ChocolateBoiler(); 017 } 018 System.out.println("Returning instance of Chocolate Boiler"); 019 return uniqueInstance; 020 } 021 022 public void fill() { 023 if (isEmpty()) { 024 empty = false; 025 boiled = false; 026 // fill the boiler with a milk/chocolate mixture 027 } 028 } 029 030 public void drain() { 031 if (!isEmpty() && isBoiled()) { 032 // drain the boiled milk and chocolate 033 empty = true; 034 } 035 } 036 037 public void boil() { 038 if (!isEmpty() && !isBoiled()) { 039 // bring the contents to a boil 040 boiled = true; 041 } 042 } 043 044 public boolean isEmpty() { 045 return empty; 046 } 047 048 public boolean isBoiled() { 049 return boiled; 050 } 051}