欢迎来到天天文库
浏览记录
ID:36497463
大小:128.50 KB
页数:8页
时间:2019-05-11
《云大设计模式实验4单件模式》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、云南大学软件学院实验报告课程:设计模式学期:2012-2013学年第一学期任课教师:陆歌浩专业:软件工程学号:20101120007姓名:孙金文成绩:实验4单件模式一、实验目的1.熟悉单件模式2.运用单件模式编制简单的java程序二、实验内容实验代码:packageheadfirst.singleton;importjavax.swing.JOptionPane;publicclassSingleton{publicstaticvoidmain(String[]args)throwsException{intinput;inp
2、ut=Integer.parseInt(JOptionPane.showInputDialog("请选择运行的单件模式的方式(1\2\3)"+"“1”代表同步方式“2”代表急切方式“3”代表双重加锁方式"));longStartTime=System.currentTimeMillis();if(input==1){test1xian1=newtest1();xian1.start1();test11xian11=newtest11();xian11.start11();}elseif(input==2){te
3、st2xian2=newtest2();xian2.start2();test22xian22=newtest22();xian22.start22();}elseif(input==3){test3xian3=newtest3();xian3.start3();test33xian33=newtest33();xian33.start33();}8longEndTime=System.currentTimeMillis();longa=EndTime-StartTime;JOptionPane.showMessageDial
4、og(null,"执行时间为:"+a+"ms.","执行结果(孙金文——20101120007)",JOptionPane.PLAIN_MESSAGE);}}packageheadfirst.singleton;publicclassChocolateboiler1{privatebooleanempty;privatebooleanboiled;privatestaticChocolateboiler1uniqueinstance;privateChocolateboiler1(){empty=true;boiled=f
5、alse;}publicstaticsynchronizedChocolateboiler1getinstance(){if(uniqueinstance==null){uniqueinstance=newChocolateboiler1();}returnuniqueinstance;}publicvoidfill(){if(isempty()){empty=false;boiled=false;}}publicvoiddrain(){if(!isempty()&&isboiled())empty=true;}publicv
6、oidboil(){if(!isempty()&&!isboiled())boiled=true;}publicbooleanisempty(){returnempty;}8publicbooleanisboiled(){returnboiled;}}packageheadfirst.singleton;publicclassChocolateboiler2{privatebooleanempty;privatebooleanboiled;privatestaticChocolateboiler2uniqueinstance=
7、newChocolateboiler2();privateChocolateboiler2(){empty=true;boiled=false;}publicstaticChocolateboiler2getinstance(){returnuniqueinstance;}publicvoidfill(){if(isempty()){empty=false;boiled=false;}}publicvoiddrain(){if(!isempty()&&isboiled())empty=true;}publicvoidboil(
8、){if(!isempty()&&!isboiled())boiled=true;}publicbooleanisempty(){returnempty;}publicbooleanisboiled(){returnboiled;}}8packageheadfirst.sin
此文档下载收益归作者所有