面向对象系统分析和设计综合实验报告4Word文档格式.docx
《面向对象系统分析和设计综合实验报告4Word文档格式.docx》由会员分享,可在线阅读,更多相关《面向对象系统分析和设计综合实验报告4Word文档格式.docx(23页珍藏版)》请在冰豆网上搜索。
instance=newIdentityCardNo();
instance。
setNo("
No6000654321”);
System.out.println("
身份证号码为:
”+instance。
getNo());
}else{
out。
println("
重复办理身份证,获取旧号码!
"
);
}
returninstance;
publicStringgetNo(){
returnno;
publicvoidsetNo(Stringno){
this.no=no;
}
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;
util.Random;
publicclassDiceClient{
privatestaticDicedie1,die2;
publicstaticvoidmain(String[]args){
die1=Dice.getInstance
(1);
die2=Dice.getInstance
(2);
System。
第一骰子骰出:
"
+die1.dice());
println(”第二骰子骰出:
”+die2。
dice());
3.某软件公司为某电影院开发了一套影院售票系统,在该系统中需要为不同类型的用户提供不同的电影票(MovieTicket)打折(Discount)方式,具体打折方案如下:
✧学生凭学生证可享受票价8折优惠;
✧年龄在10周岁及以下的儿童可享受每张票减免10元的优惠(原始票价需大于等于20元);
✧影院VIP用户除享受票价半价优惠外还可进行积分,积分累计到一定额度可换取电影院赠送的奖品.
该系统在将来可能还要根据需要引入新的打折方式.
试使用策略模式设计并编程模拟实现该影院售票系统。
publicinterfaceDiscount{
publicdoublecalculate(doubleprice);
publicclassMovieTicket{
privatedoubleprice;
privateDiscountdiscount;
//维持一个对抽象折扣类的引用
publicvoidsetPrice(doubleprice){
this。
price=price;
}
//注入一个折扣类对象
publicvoidsetDiscount(Discountdiscount){
discount=discount;
}
publicdoublegetPrice(){
//调用折扣类的折扣价计算方法
returndiscount.calculate(this。
price);
}
//VIP会员票折扣类:
具体策略类
publicclassVIPDiscountimplementsDiscount{
publicdoublecalculate(doubleprice){
System。
VIP票:
”);
System.out。
println(”增加积分!
returnprice*0。
5;
//学生票折扣类:
publicclassStudentDiscountimplementsDiscount{
System.out.println(”学生票:
);
8;
}
//儿童票折扣类:
publicclassChildrenDiscountimplementsDiscount{
println(”儿童票:
returnprice-10;
publicclassMoviceClient{
publicstaticvoidmain(Stringargs[]){
MovieTicketmt=newMovieTicket();
doubleoriginalPrice=60.0;
doublecurrentPrice;
mt。
setPrice(originalPrice);
System.out.println("
原始价为:
+originalPrice);
—---—---—-————--—-—-———-—---—--—-”);
Discountdiscount=newVIPDiscount();
//vip用户
setDiscount(discount);
//注入折扣对象
currentPrice=mt.getPrice();
out.println(”折后价为:
”+currentPrice);
discount=newStudentDiscount();
//学生用户
setDiscount(discount);
currentPrice=mt。
getPrice();
折后价为:
+currentPrice);
discount=newChildrenDiscount();
//儿童用户
mt.setDiscount(discount);
println(”折后价为:
3)实现结果:
4。
某软件公司欲开发一款飞机模拟系统,该系统主要模拟不同种类飞机的飞行特征与起飞特征,需要模拟的飞机种类及其特征如表1所示:
表1飞机种类及特征一览表
飞机种类
起飞特征
飞行特征
直升机(Helicopter)
垂直起飞(VerticalTakeOff)
亚音速飞行(SubSonicFly)
客机(AirPlane)
长距离起飞(LongDistanceTakeOff)
歼击机(Fighter)
超音速飞行(SuperSonicFly)
鹞式战斗机(Harrier)
为将来能够模拟更多种类的飞机,试采用策略模式设计并模拟实现该飞机模拟系统.
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(){
fly();
publicclassAirPlaneimplementsstate{
@Override
publicStringtakeOff(){
println(”长距离起飞”);
return”长距离起飞”;
publicStringfly(){
亚音速飞行"
return"
;
publicclassFighterimplementsstate{
publicStringtakeOff(){
长距离起飞”);
@Override
publicStringfly(){
超亚音速飞行"
超音速飞行”;
publicclassHarrierimplementsstate{
System.out.println("
垂直起飞"
;
println(”超亚音速飞行"
return”超音速飞行"
publicclassHelicopterimplementsstate{
System.out。
println(”垂直起飞"
return”垂直起飞”;
亚音速飞行”);
return”亚音速飞行”;
publicinterfacestate{
publicStringtakeOff();
//起飞
publicStringfly();
//飞行
publicclassClient{
publicstaticvoidmain(Stringargs[]){
planeplane=newplane();
plane.setplanetype("
歼击机"
plane.takeoff();
plane.fly();
5。
儿子、妈妈、父亲三人合作画一幅画,儿子负责画出一朵花的轮廓,妈妈负责涂上颜色、父亲负责将画裱在画框里。
现请使用装饰模式模拟这个过程。
2)实现代码:
publicinterfacepainting{
publicStringDraw();
publicclassSonimplementspainting{
publicStringDraw(){
儿子用笔画出了花的轮廓"
return”儿子用笔画出了花的轮廓”;
publicclassFatherimplementspainting{
privatepaintingpainting;
//被装饰者
publicFather(paintingpainting){
this.painting=painting;
privateFather(){}
publicvoidpaint(){
//爸爸装饰者做的职责
System.out.println(”爸爸正在做上画框前的准备工作”);
painting。
Draw();
//爸爸装饰者做职责
println(”父亲将画裱在画框里”);
publicStringDraw(){
父亲将画裱在画框里"
父亲将画裱在画框里”;
publicclassMotherimplementspainting{
privatepaintingpainting;
publicMother(paintingpainting){
painting=painting;
privateMother(){}
publicvoidpaint(){
妈妈正在做给画上颜色前的准备工作。
painting.Draw();
//妈妈装饰者做的职责
妈妈给画上好了颜色"
println(”妈妈给画上好了颜色"
return”妈妈给画上好了颜色"
publicstaticvoidmain(String[]args){
paintingpainting=newSon();
painting。
Draw();
painting=newMother(painting);
painting.Draw();
painting=newFather(painting);
3)实现结果:
6。
某公司想通过网络传输数据,但是担心文件被窃取。
他们的所有数据都采用字符的方式传送。
现在他们开发了一个数据加密模块,可以对字符串进行加密,以便数据更安全地传送.最简单的加密算法通过对字母向后移动6位来实现,同时还提供了稍复杂的逆向输出加密,还提供了更为高级的求模加密,让每一位与6求模。
用户先使用最简单的加密算法对字符串进行加密,再对加密之后的结果使用复杂加密算法进行二次加密,再对二次加密结果用高级加密算法进行第三次加密。
publicclassConcreteEncryptimplementsEncryptComponet{
privateEncryptComponetencryptComponet;
publicConcreteEncrypt(EncryptComponetencryptComponet){
super();
this.encryptComponet=encryptComponet;
publicvoidencrypt(){
encryptComponet.encrypt();
publicinterfaceEncryptComponet{
publicabstractvoidencrypt();
publicclassRawDataimplementsEncryptComponet{
publicvoidencrypt(){
println(”这是要发送的数据"
publicclassReversEncryptimplementsEncryptComponet{
publicReversEncrypt(EncryptComponetencryptComponet){
addReservesEncrypt();
privatevoidaddReservesEncrypt(){
反向加密”);
publicvoidencrypt(){
publicclassSuperEncryptimplementsEncryptComponet{
publicSuperEncrypt(EncryptComponetencryptComponet){
addSuperEncrypt();
privatevoidaddSuperEncrypt(){
最高加密算法”);
publicvoidencrypt(){
publicclassTranslateEncryptimplementsEncryptComponet{
publicTranslateEncrypt(EncryptComponetencryptComponet){
addTranslateEncrypt();
privatevoidaddTranslateEncrypt(){
System.out.println(”移动加密"
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()等。
如果希望在不修改已有代码的基础上使得机器人能够像狗一样叫,像狗一样跑,使用适配器模式进行系统设计.
2)实现代码
publicinterfaceRobot{
publicvoidcry();
publicvoidmove();
publicclassDog{
publicvoidbarks(){
println(”狗在叫”);
publicvoidrun(){
狗在跑”);
publicclassDogadapterextendsDogimplementsRobot{
publicvoidcry(){
barks();
publicvoidmove(){
run();
publicstaticvoidmain(String[]args){
Dogadapte