设计模式应用实践各种模式典型例子代码.docx
- 文档编号:7061358
- 上传时间:2023-01-16
- 格式:DOCX
- 页数:55
- 大小:348.10KB
设计模式应用实践各种模式典型例子代码.docx
《设计模式应用实践各种模式典型例子代码.docx》由会员分享,可在线阅读,更多相关《设计模式应用实践各种模式典型例子代码.docx(55页珍藏版)》请在冰豆网上搜索。
设计模式应用实践各种模式典型例子代码
课程名称:
计模式应用实践
任课教师:
学号:
姓名:
高原
院系:
实验序号
成绩
实验1
10
实验2
10
实验3
10
实验4
10
实验5
10
实验6
10
实验7
10
实验8
10
总成绩
重庆理工大学
2011.03月软件工程系制
1.实验一:
程序设计模式应用实践1
1实验时间:
2011年3月19日
2实验地点:
重庆理工大学第四教学楼
3实验要求或内容:
思考完成“教学材料”“多态”里面的思考题,写出实验报告(按照模板)
在这里给出输出结果
4程序设计与代码(要有注释,最好有设计类图):
代码结构:
程序代码:
classA{
publicStringshow(Dobj){
return("AandD");
}
publicStringshow(Aobj){
return("AandA");
}
}
classBextendsA{
publicStringshow(Bobj){
return("BandB");
}
publicStringshow(Aobj){
return("BandA");
}
}
classCextendsB{}
classDextendsB{}
publicclassTest{
publicstaticvoidmain(String[]args){
Aa1=newA();
Aa2=newB();//用newB()个new了A对象,实质上a2还是A对象。
调用时还是调用A中的方法,如果A中方法没有则在B中找;如果找到了,就调用A中的方法,如果A中的方法被重写,则调用B中重写的方法。
Bb=newB();
Cc=newC();
Dd=newD();
System.out.println(a1.show(b));
System.out.println(a1.show(c));
System.out.println(a1.show(d));
System.out.println(a2.show(b));//调用时传的是b先从A中找,找到了show(Aobj)方法(注:
b是A的子类),但是被B覆盖了,则调用B中的show(Aobj)方法,所以打印出BandA
System.out.println(a2.show(c));//调用时传的是c先从A中找,找到了show(Aobj)方法(注:
c是A的子类),但是被B覆盖了,则调用B中的show(Aobj)方法,所以打印出BandA
System.out.println(a2.show(d));//调用时传的是d先从A中找,找到了show(Dobj)方法,所以打印出AandD
System.out.println(b.show(b));
System.out.println(b.show(c));
System.out.println(b.show(d));
}
}
5运行结果:
6实验心得:
2.实验二:
程序设计模式应用实践2
1实验时间:
2011年5月21日
2实验地点:
重庆理工大学第四教学楼
3实验要求或内容:
首先设计学生类和教师类的测试设计,根据设计思想生成这两个类,并 利用JUnit4生成相应的测试类,运行测试
1)写入实验报告
2)将测试类代码及运行结果提交教学平台
4程序设计与代码(要有注释,最好有设计类图):
代码结构:
一、学生类测试运行结果截图:
学生类代码:
Student.java
packageedu.cqut;
publicclassStudent{
Stringname;
intscore;
Stringgrade;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetScore(){
returnscore;
}
publicvoidsetScore(intscore){
this.score=score;
}
publicStringgetGrade(){
if(score>=90)
return"优秀";
if(score>=80)
return"良好";
if(score>=70)
return"中等";
if(score>=60)
return"及格";
return"不及格";
}
publicStringtoString(){
returnname+":
"+getGrade();
}
}
测试类代码:
StudentTest.java
教师类代码:
Teacher.java
packageedu.cqut;
publicclassTeacher{
Stringname;
intwage;
Stringtitles;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetWage(){
returnwage;
}
publicvoidsetWage(intwage){
this.wage=wage;
}
publicStringgetTitles(){
if(wage>=8000)
return"教授";
if(wage>=6000)
return"副教授";
if(wage>=3000)
return"讲师";
return"助教";
}
}
教师类测试代码:
TeacherTest.java
packageedu.cqut;
importstaticorg.junit.Assert.*;
importjava.util.*;
importorg.junit.*;
importorg.junit.runner.RunWith;
importorg.junit.runners.Parameterized;
importorg.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
publicclassTeacherTest{
Teachert=newTeacher();
privateStringname;
privateintwage;
privateStringtitles;
publicTeacherTest(Stringname,Stringtitles,intwage){
this.name=name;
this.titles=titles;
this.wage=wage;
}
@Parameters
publicstaticCollectiontestCases(){
returnArrays.asList(newObject[][]{
{"夏金","教授",9000},
{"周贤","副教授",7000},
{"王潇","讲师",4000},
{"李超","助教",2000},
});
}
@BeforeClass
publicstaticvoidsetUpBeforeClass()throwsException{
}
@AfterClass
publicstaticvoidtearDownAfterClass()throwsException{
}
@Before
publicvoidsetUp()throwsException{
System.out.println("start");
t.setWage(wage);
t.setName(name);
}
@After
publicvoidtearDown()throwsException{
System.out.println("end");
}
@Test
publicvoidtestGetTitles(){
//fail("Notyetimplemented");
System.out.println("testGetTitles");
assertEquals(titles,t.getTitles());
}
}
packageedu.cqut;
importstaticorg.junit.Assert.*;
importjava.util.*;
importorg.junit.*;
importorg.junit.runner.RunWith;
importorg.junit.runners.Parameterized;
importorg.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
publicclassStudentTest{
Students=newStudent();
privateStringgrade;
privateintscore;
privateStringname;
publicStudentTest(Stringname,Stringgrade,intscore){
this.grade=grade;
this.score=score;
this.name=name;
}
@Parameters
publicstaticCollectiontestCases(){
returnArrays.asList(newObject[][]{
{"夏雨","优秀",95},
{"李四","良好",85},
{"王二","中等",75},
{"张三","及格",65},
{"周迅","不及格",50},
});
}
@BeforeClass
publicstaticvoidsetUpBeforeClass()throwsException{
}
@AfterClass
publicstaticvoidtearDownAfterClass()throwsException{
}
@Before
publicvoidsetUp()throwsException{
System.out.println("start");
s.setScore(score);
s.setName(name);
}
@After
publicvoidtearDown()throwsException{
System.out.println("end");
}
@Test
publicvoidtestGetGrade(){
//fail("Notyetimplemented");
System.out.println("testGetGrade");
assertEquals(grade,s.getGrade());
}
@Test
publicvoidtestToString(){
//fail("Notyetimplemented");
System.out.println("testToString");
assertEquals(name+":
"+grade,s.toString());
}
}
5运行结果:
学生类测试运行结果截图:
教师类测试运行结果截图:
6实验心得:
3.实验三:
程序设计模式应用实践上机3
1实验时间:
2011年4月2日
2实验地点:
重庆理工大学第四教学楼
3实验要求或内容:
实现简单工厂模式的三种工厂(以上课讲的前面2种为主)
4程序设计与代码(要有注释,最好有设计类图):
类图:
代码结构:
Weapon类接口:
packagefactory;
publicinterfaceweapon{//武器类接口下面各种武器实现此接口
publicvoidFire();
}
F16类代码:
packagefactory;
publicclassF16implementsweapon{
publicvoidFire(){
System.out.println("F16 开火!
");
}
}
M2A1类代码:
packagefactory;
publicclassM1A1implementsweapon{
publicvoidFire(){
System.out.println("M1A1 开火!
");
}
}
AK47类代码:
packagefactory;
publicclassAK47implementsweapon{
publicvoidFire(){
System.out.println("AK47 开火!
");
}
}
工厂类1代码:
packagefactory;
publicclassweaponFactory1{//传入字符串通过比较字符串来确定生产那种实例
publicstaticweaponcreateWeapon(Stringtype){
if(type.equalsIgnoreCase("F16")){
returnnewF16();
}
if(type.equalsIgnoreCase("AK47")){
returnnewAK47();
}
if(type.equalsIgnoreCase("M1A1")){
returnnewM1A1();
}
Returnnull;
}
}
工厂类2代码:
packagefactory;
publicclassweaponFactory2{//传入字符串通过ClassforName来判断生成哪个类的实例
publicstaticweaponcreateWeapon(Stringtype){
weaponw=null;
try{
w=(weapon)Class.forName("factory."+type).newInstance();
}catch(InstantiationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnw;
}
}
工厂类3代码:
packagefactory;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.Properties;
publicclassweaponFactory3{//通过配置文件确定生成哪个实例
publicstaticweaponcreateWeapon(Stringtype){
weaponw=null;
Stringt=null;
Propertiesp=newProperties();
try{
p.load(newFileInputStream("src//config.properties"));
t=p.getProperty(type);
}catch(FileNotFoundExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
w=(weapon)Class.forName(t).newInstance();
}catch(InstantiationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IllegalAccessExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnw;
}
}
工厂类3的配置文件:
AK47=factory.AK47
F16=factory.F16
M1A1=factory.M1A1
客户端代码:
packageclient;
importfactory.weaponFactory1;
importfactory.weaponFactory2;
importfactory.weaponFactory3;
publicclassclient{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
weaponFactory1.createWeapon("F16").Fire();
weaponFactory2.createWeapon("AK47").Fire();
weaponFactory3.createWeapon("M1A1").Fire();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 设计模式应用实践 各种模式典型例子代码 设计 模式 应用 实践 各种 典型 例子 代码