编辑: 鱼饵虫 | 2019-09-19 |
题目: 分别使用同步getInstance()方法、急切实例化、双重检查加锁三种方法实现课本中的巧克力锅炉. 2.模式设计的UML类图: 3.程序源代码: (1)巧克力锅炉类Boiler.java: package singleton;
public class Boiler { protected boolean empty;
protected boolean boiled;
public void fill() { if(isEmpty()) { empty = false;
boiled = false;
} } public String drain() { if(!isEmpty() && isBoiled()) { empty = true;
return "咖啡和牛奶已经煮好了.";
} else return "咖啡和牛奶没有煮好.";
} public void boil() { if(!isEmpty() && !isBoiled()) { boiled = true;
} } public boolean isEmpty() { return empty;
} public boolean isBoiled() { return boiled;
} } (2)同步方法实现巧克力锅炉的类SynchronizedChocolateBoiler.java: package singleton;
public class SynchronizedChocolateBoiler extends Boiler { private static SynchronizedChocolateBoiler uniqueInstance;
private SynchronizedChocolateBoiler() { empty = true;
boiled = false;
} public static synchronized SynchronizedChocolateBoiler getInstance() { if(uniqueInstance == null) { uniqueInstance = new SynchronizedChocolateBoiler();
} return uniqueInstance;
} } (3)急切实例化实现巧克力锅炉的类EagerlyChocolateBoiler.java: package singleton;
public class EagerlyChocolateBoiler extends Boiler { private static EagerlyChocolateBoiler uniqueInstance = new EagerlyChocolateBoiler();
private EagerlyChocolateBoiler() { empty = true;
boiled = false;
} public static EagerlyChocolateBoiler getInstance() { return uniqueInstance;
} } (4)双重检查加锁实现巧克力锅炉的类DCLChocolateBoiler.java: package singleton;
public class DCLChocolateBoiler extends Boiler { private volatile static DCLChocolateBoiler uniqueInstance;
private DCLChocolateBoiler() { empty = true;
boiled = false;
} public static DCLChocolateBoiler getInstance() { if(uniqueInstance == null) { synchronized(DCLChocolateBoiler.class) { if(uniqueInstance == null) { uniqueInstance = new DCLChocolateBoiler();
} } } return uniqueInstance;
} } (5)实现用户界面的主类ChocolateBoiler.java: (由于代码过长,只列出重要部分) package singleton;
public class ChocolateBoiler extends javax.swing.JFrame { public ChocolateBoiler() { super(XXXXXX);
initComponents();
} private void initComponents() { jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("锅炉实现类型"));
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("同步实现");
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("急切实例化");
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("双重检查加锁");
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("咖啡加工"));
buttonGroup2.add(jRadioButton4);
jRadioButton4.setText("加料");
buttonGroup3.add(jRadioButton5);
jRadioButton5.setText("煮沸");
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("消息输出"));
jLabel1.setText("消息:");
jTextField1.setText("");
jButton1.setText("退出");
jButton2.setText("清空");
jButton3.setText("确定");
}// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0);
} private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText("");
buttonGroup1.clearSelection();
buttonGroup2.clearSelection();
buttonGroup3.clearSelection();
buttonGroup4.clearSelection();
} private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { String[] str = Boiler();
String infor = str[0]str[1];
jTextField1.setText(infor);
} public String[] Boiler() { String[] str = new String[2];
Boiler boiler = null;
if(jRadioButton1.isSelected()) { boiler = SynchronizedChocolateBoiler.getInstance();
str[1] = "(同步实现)";
} else if(jRadioButton2.isSelected()) { boiler = EagerlyChocolateBoiler.getInstance();
str[1] = "(急切实例化)";
} else if(jRadioButton3.isSelected()) { boiler = DCLChocolateBoiler.getInstance();
str[1] = "(双重检查加锁)";
} if(jRadioButton4.isSelected()&&!jRadioButton5.isSelected()) { boiler.boil();
str[0] = boiler.drain();
} else if(jRadioButton5.isSelected()&&!jRadioButton4.isSelected()) { boiler.fill();
str[0] = boiler.drain();
} else if(jRadioButton4.isSelected()&&jRadioButton5.isSelected()) { boiler.fill();
boiler.boil();
str[0] = boiler.drain();
} else if(!jRadioButton4.isSelected()&&!jRadioButton5.isSelected()) { str[0] = boiler.drain();
} return str;
} public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ChocolateBoiler().setVisible(true);
} });
} } 4.实验结果截图: