面向对象系统分析和设计综合实验报告4.docx

上传人:b****1 文档编号:10304154 上传时间:2023-05-24 格式:DOCX 页数:25 大小:199.37KB
下载 相关 举报
面向对象系统分析和设计综合实验报告4.docx_第1页
第1页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第2页
第2页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第3页
第3页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第4页
第4页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第5页
第5页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第6页
第6页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第7页
第7页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第8页
第8页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第9页
第9页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第10页
第10页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第11页
第11页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第12页
第12页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第13页
第13页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第14页
第14页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第15页
第15页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第16页
第16页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第17页
第17页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第18页
第18页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第19页
第19页 / 共25页
面向对象系统分析和设计综合实验报告4.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

面向对象系统分析和设计综合实验报告4.docx

《面向对象系统分析和设计综合实验报告4.docx》由会员分享,可在线阅读,更多相关《面向对象系统分析和设计综合实验报告4.docx(25页珍藏版)》请在冰点文库上搜索。

面向对象系统分析和设计综合实验报告4.docx

面向对象系统分析和设计综合实验报告4

实验名称:

实验4设计模型实验2学期:

2017-2018学年第二学期

一、实验目的

1.熟练使用面向对象设计原则对系统进行重构;

2.熟练使用面向对象编程语言(JAVA或C++)实现几种常见的设计模式,包括单例模式、策略模式、装饰模式和适配器模式,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些模式。

二、实验要求

1.选择合适的面向对象设计原则对系统进行重构,正确无误地绘制重构之后的类图;

2.结合实例,正确无误地绘制单例模式、策略模式、装饰模式和适配器模式的结构图;

3.实现单例模式、策略模式、装饰模式和适配器模式,代码运行正确无误。

三、实验内容

1.现实生活中,居民身份证号码具有唯一性,同一个人不允许有多个身份证号码,第一次申请身份证时将号码分配给居民,如果之后因为遗失等原因补办时,还是使用原来的身份证号码,不会产生新号码,现使用单例模式模拟该场景。

1)类图

 

2)实现代码:

publicclassIdClient{

publicstaticvoidmain(String[]args){

IdentityCardNo.getInstance();

IdentityCardNo.getInstance();

}

}

packageRefactoring1;

publicclassIdentityCardNo{

privatestaticIdentityCardNoinstance;

privateStringno;

privateIdentityCardNo(){

}

publicstaticIdentityCardNogetInstance(){

if(instance==null){

System.out.println("第一次办理身份证,分配新号码");

instance=newIdentityCardNo();

instance.setNo("No6000654321");

System.out.println("身份证号码为:

"+instance.getNo());

}else{

System.out.println("重复办理身份证,获取旧号码!

");

}

returninstance;

}

publicStringgetNo(){

returnno;

}

publicvoidsetNo(Stringno){

this.no=no;

}

}

 

2.每一麻将局都有两个骰子,因此骰子就应当是双例类。

现使用多例模式模拟该场景。

1)类图

 

2)实现代码:

importjava.util.Date;

importjava.util.Random;

publicclassDice{

privatestaticDicedie1=newDice();

privatestaticDicedie2=newDice();

privateDice(){

}

publicstaticDicegetInstance(intwhichOne){

if(whichOne==1){

returndie1;

}else{

returndie2;

}

}

publicsynchronizedintdice(){

Dated=newDate();

Randomr=newRandom(d.getTime());

intvalue=r.nextInt();

value=Math.abs(value);

value=value%6;

value+=1;

returnvalue;

}

}

importjava.util.Random;

importjava.util.Date;

publicclassDiceClient{

privatestaticDicedie1,die2;

publicstaticvoidmain(String[]args){

die1=Dice.getInstance

(1);

die2=Dice.getInstance

(2);

System.out.println("第一骰子骰出:

"+die1.dice());

System.out.println("第二骰子骰出:

"+die2.dice());

}

}

3.某软件公司为某电影院开发了一套影院售票系统,在该系统中需要为不同类型的用户提供不同的电影票(MovieTicket)打折(Discount)方式,具体打折方案如下:

✧学生凭学生证可享受票价8折优惠;

✧年龄在10周岁及以下的儿童可享受每张票减免10元的优惠(原始票价需大于等于20元);

✧影院VIP用户除享受票价半价优惠外还可进行积分,积分累计到一定额度可换取电影院赠送的奖品。

该系统在将来可能还要根据需要引入新的打折方式。

试使用策略模式设计并编程模拟实现该影院售票系统。

1)类图

 

2)实现代码:

publicinterfaceDiscount{

publicdoublecalculate(doubleprice);

}

publicclassMovieTicket{

privatedoubleprice;

privateDiscountdiscount;//维持一个对抽象折扣类的引用

publicvoidsetPrice(doubleprice){

this.price=price;

}

//注入一个折扣类对象

publicvoidsetDiscount(Discountdiscount){

this.discount=discount;

}

publicdoublegetPrice(){

//调用折扣类的折扣价计算方法

returndiscount.calculate(this.price);

}

}

//VIP会员票折扣类:

具体策略类

publicclassVIPDiscountimplementsDiscount{

publicdoublecalculate(doubleprice){

System.out.println("VIP票:

");

System.out.println("增加积分!

");

returnprice*0.5;

}

}

//学生票折扣类:

具体策略类

publicclassStudentDiscountimplementsDiscount{

publicdoublecalculate(doubleprice){

System.out.println("学生票:

");

returnprice*0.8;

}

}

//儿童票折扣类:

具体策略类

publicclassChildrenDiscountimplementsDiscount{

publicdoublecalculate(doubleprice){

System.out.println("儿童票:

");

returnprice-10;

}

}

publicclassMoviceClient{

publicstaticvoidmain(Stringargs[]){

MovieTicketmt=newMovieTicket();

doubleoriginalPrice=60.0;

doublecurrentPrice;

mt.setPrice(originalPrice);

System.out.println("原始价为:

"+originalPrice);

System.out.println("---------------------------------");

Discountdiscount=newVIPDiscount();//vip用户

mt.setDiscount(discount);//注入折扣对象

currentPrice=mt.getPrice();

System.out.println("折后价为:

"+currentPrice);

discount=newStudentDiscount();//学生用户

mt.setDiscount(discount);//注入折扣对象

currentPrice=mt.getPrice();

System.out.println("折后价为:

"+currentPrice);

discount=newChildrenDiscount();//儿童用户

mt.setDiscount(discount);//注入折扣对象

currentPrice=mt.getPrice();

System.out.println("折后价为:

"+currentPrice);

}

}

3)实现结果:

 

4.某软件公司欲开发一款飞机模拟系统,该系统主要模拟不同种类飞机的飞行特征与起飞特征,需要模拟的飞机种类及其特征如表1所示:

表1飞机种类及特征一览表

飞机种类

起飞特征

飞行特征

直升机(Helicopter)

垂直起飞(VerticalTakeOff)

亚音速飞行(SubSonicFly)

客机(AirPlane)

长距离起飞(LongDistanceTakeOff)

亚音速飞行(SubSonicFly)

歼击机(Fighter)

长距离起飞(LongDistanceTakeOff)

超音速飞行(SuperSonicFly)

鹞式战斗机(Harrier)

垂直起飞(VerticalTakeOff)

超音速飞行(SuperSonicFly)

为将来能够模拟更多种类的飞机,试采用策略模式设计并模拟实现该飞机模拟系统。

1)类图

 

2)实现代码:

publicclassplane{

privatestatestate;//状态

publicvoidsettakeoffFeatures(statetFeatures){

this.state=tFeatures;

}

publicvoidsetplanetype(Stringtype){

if(type=="直升机")

state=newHelicopter();

elseif(type=="客机"){

state=newAirPlane();

}

elseif(type=="歼击机"){

state=newFighter();

}

elseif(type=="鹞式战斗机"){

state=newHarrier();

}

else{

state=null;

}

}

publicvoidtakeoff(){

state.takeOff();

}

publicvoidfly(){

state.fly();

}

}

publicclassAirPlaneimplementsstate{

@Override

publicStringtakeOff(){

System.out.println("长距离起飞");

return"长距离起飞";

}

@Override

publicStringfly(){

System.out.println("亚音速飞行");

return"亚音速飞行";

}

}

publicclassFighterimplementsstate{

@Override

publicStringtakeOff(){

System.out.println("长距离起飞");

return"长距离起飞";

}

@Override

publicStringfly(){

System.out.println("超亚音速飞行");

return"超音速飞行";

}

}

publicclassHarrierimplementsstate{

@Override

publicStringtakeOff(){

System.out.println("垂直起飞");

return"垂直起飞";

}

@Override

publicStringfly(){

System.out.println("超亚音速飞行");

return"超音速飞行";

}

}

publicclassHelicopterimplementsstate{

@Override

publicStringtakeOff(){

System.out.println("垂直起飞");

return"垂直起飞";

}

@Override

publicStringfly(){

System.out.println("亚音速飞行");

return"亚音速飞行";

}

}

publicinterfacestate{

publicStringtakeOff();//起飞

publicStringfly();//飞行

}

publicclassClient{

publicstaticvoidmain(Stringargs[]){

planeplane=newplane();

plane.setplanetype("歼击机");

plane.takeoff();

plane.fly();

}

}

 

3)实现结果:

5.儿子、妈妈、父亲三人合作画一幅画,儿子负责画出一朵花的轮廓,妈妈负责涂上颜色、父亲负责将画裱在画框里。

现请使用装饰模式模拟这个过程。

 

1)类图

 

 

2)实现代码:

publicinterfacepainting{

publicStringDraw();

}

publicclassSonimplementspainting{

@Override

publicStringDraw(){

System.out.println("儿子用笔画出了花的轮廓");

return"儿子用笔画出了花的轮廓";

}

}

publicclassFatherimplementspainting{

privatepaintingpainting;//被装饰者

publicFather(paintingpainting){

this.painting=painting;

}

privateFather(){}

publicvoidpaint(){

//爸爸装饰者做的职责

System.out.println("爸爸正在做上画框前的准备工作");

painting.Draw();//爸爸装饰者做职责

System.out.println("父亲将画裱在画框里");

}

@Override

publicStringDraw(){

System.out.println("父亲将画裱在画框里");

return"父亲将画裱在画框里";

}

}

publicclassMotherimplementspainting{

privatepaintingpainting;//被装饰者

publicMother(paintingpainting){

this.painting=painting;

}

privateMother(){}

publicvoidpaint(){

System.out.println("妈妈正在做给画上颜色前的准备工作。

");

painting.Draw();//妈妈装饰者做的职责

System.out.println("妈妈给画上好了颜色");

}

@Override

publicStringDraw(){

System.out.println("妈妈给画上好了颜色");

return"妈妈给画上好了颜色";

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

paintingpainting=newSon();

painting.Draw();

painting=newMother(painting);

painting.Draw();

painting=newFather(painting);

painting.Draw();}

}

3)实现结果:

 

6.某公司想通过网络传输数据,但是担心文件被窃取。

他们的所有数据都采用字符的方式传送。

现在他们开发了一个数据加密模块,可以对字符串进行加密,以便数据更安全地传送。

最简单的加密算法通过对字母向后移动6位来实现,同时还提供了稍复杂的逆向输出加密,还提供了更为高级的求模加密,让每一位与6求模。

用户先使用最简单的加密算法对字符串进行加密,再对加密之后的结果使用复杂加密算法进行二次加密,再对二次加密结果用高级加密算法进行第三次加密。

现请使用装饰模式模拟这个过程。

 

1)类图

 

2)实现代码:

publicclassConcreteEncryptimplementsEncryptComponet{

privateEncryptComponetencryptComponet;

publicConcreteEncrypt(EncryptComponetencryptComponet){

super();

this.encryptComponet=encryptComponet;

}

publicvoidencrypt(){

encryptComponet.encrypt();

}

}

publicinterfaceEncryptComponet{

publicabstractvoidencrypt();

}

publicclassRawDataimplementsEncryptComponet{

publicvoidencrypt(){

System.out.println("这是要发送的数据");

}

}

publicclassReversEncryptimplementsEncryptComponet{

publicReversEncrypt(EncryptComponetencryptComponet){

addReservesEncrypt();

}

privatevoidaddReservesEncrypt(){

System.out.println("反向加密");

}

@Override

publicvoidencrypt(){

}

}

publicclassSuperEncryptimplementsEncryptComponet{

publicSuperEncrypt(EncryptComponetencryptComponet){

addSuperEncrypt();

}

privatevoidaddSuperEncrypt(){

System.out.println("最高加密算法");

}

@Override

publicvoidencrypt(){

}

}

publicclassTranslateEncryptimplementsEncryptComponet{

publicTranslateEncrypt(EncryptComponetencryptComponet){

addTranslateEncrypt();

}

privatevoidaddTranslateEncrypt(){

System.out.println("移动加密");

}

@Override

publicvoidencrypt(){

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

EncryptComponets0,s1,s2,s3;

s0=newRawData();

s1=newTranslateEncrypt(s0);

s2=newReversEncrypt(s1);

s3=newSuperEncrypt(s2);

s3.encrypt();

}

}

7.现需要设计一个可以模拟各种动物行为的机器人,在机器人中定义了一系列方法,如机器人叫喊方法cry()、机器人移动方法move()等。

如果希望在不修改已有代码的基础上使得机器人能够像狗一样叫,像狗一样跑,使用适配器模式进行系统设计。

1)类图

2)实现代码

publicinterfaceRobot{

publicvoidcry();

publicvoidmove();

}

publicclassDog{

publicvoidbarks(){

System.out.println("狗在叫");

}

publicvoidrun(){

System.out.println("狗在跑");

}

}

publicclassDogadapterextendsDogimplementsRobot{

@Override

publicvoidcry(){

barks();

}

@Override

publicvoidmove(){

run();

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

Dogadap

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 哲学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2