单件模式

单件模式

云南大学软件学院实验报告课程:设计模式实验学期:2010-2011学年第一学期任课教师:陆歌皓专业:软件工程学号:20081120072 姓名:都鹏成绩:

实验5 单件模式

一、实验题目

分别使用同步方法、急切实例化、双重检查加锁方法实现课本中的巧克力锅炉。二、模式设计UML类图:

单件模式

三、程序代码:

Boiler类

public class Boiler {

protected boolean empty;

protected boolean boiled;

public void fill(){

if(isEmpty()){

empty =false;

boiled = true;

}

}

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;

}

}

DCLChocolateBoiler类

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;

}

}

EagerlyChocolateBoiler类

public class EagerlyChocolateBoiler extends Boiler{//急切实例化private static EagerlyChocolateBoiler uniqueInstance=new EagerlyChocolateBoiler();

private EagerlyChocolateBoiler() {

empty = true;

boiled = false;

}

public static EagerlyChocolateBoiler getInstance() { return uniqueInstance;

}

}

synchronizedChocolateBoiler类

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;

}

}

Chocolate类

public class Chocolate extends javax.swing.JFrame {

private static final long serialVersionUID = 5659781085817976205L;

public Chocolate() {

initComponents();

}

private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();

jPanel1 = new javax.swing.JPanel();

jPanel2 = new javax.swing.JPanel();

jRadioButton1 = new javax.swing.JRadioButton();

jRadioButton2 = new javax.swing.JRadioButton();

jRadioButton3 = new javax.swing.JRadioButton();

jPanel3 = new javax.swing.JPanel();

jButton1 = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

jTextArea1 = new javax.swing.JTextArea();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

------------------略----------------------

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String p = Boiler();

j TextArea1.setText(p);

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { j TextArea1.setText(null);

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0);

}

public String Boiler(){

B oiler boiler = null;

S tring str = null;

i f(jRadioButton1.isSelected()){

boiler =synchronizedChocolateBoiler.getInstance();

str = boiler.drain()+"(同步实现)";

}

e lse if(jRadioButton2.isSelected()){

boiler =EagerlyChocolateBoiler.getInstance();

str =boiler.drain()+"(急切实例化)";

}

e lse if(jRadioButton3.isSelected()){

boiler =DCLChocolateBoiler.getInstance();

str =boiler.drain()+"(双重检查加锁)";

}

r eturn str;

}

private javax.swing.ButtonGroup buttonGroup1;

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JPanel jPanel1;

private javax.swing.JPanel jPanel2;

private javax.swing.JPanel jPanel3;

private javax.swing.JRadioButton jRadioButton1;

private javax.swing.JRadioButton jRadioButton2;

private javax.swing.JRadioButton jRadioButton3;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTextArea jTextArea1;

}

四、运行结果:

单件模式

相关推荐
相关主题
热门推荐