分享
分销 收藏 举报 申诉 / 18
播放页_导航下方通栏广告

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

  • 上传人:w****g
  • 文档编号:3770017
  • 上传时间:2024-07-17
  • 格式:DOC
  • 页数:18
  • 大小:492.26KB
  • 下载积分:8 金币
  • 播放页_非在线预览资源立即下载上方广告
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    面向 对象 系统分析 设计 综合 实验 报告
    资源描述:
    西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 实验名称:实验4 设计模型实验2 学期:2017-2018学年 第二学期 一、实验目的 1.熟练使用面向对象设计原则对系统进行重构; 2.熟练使用面向对象编程语言(JAVA或C++)实现几种常见的设计模式,包括单例模式、策略模式、装饰模式和适配器模式,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些模式。 二、实验要求 1. 选择合适的面向对象设计原则对系统进行重构,正确无误地绘制重构之后的类图; 2. 结合实例,正确无误地绘制单例模式、策略模式、装饰模式和适配器模式的结构图; 3. 实现单例模式、策略模式、装饰模式和适配器模式,代码运行正确无误。 三、实验内容 1. 现实生活中,居民身份证号码具有唯一性,同一个人不允许有多个身份证号码,第一次申请身份证时将号码分配给居民,如果之后因为遗失等原因补办时,还是使用原来的身份证号码,不会产生新号码,现使用单例模式模拟该场景。 1) 类图 2) 实现代码: public class IdClient { public static void main(String[] args) { IdentityCardNo.getInstance(); IdentityCardNo.getInstance(); } } package Refactoring1; public class IdentityCardNo { private static IdentityCardNo instance; private String no; private IdentityCardNo() { } public static IdentityCardNo getInstance() { if (instance == null) { System.out.println("第一次办理身份证,分配新号码"); instance = new IdentityCardNo(); instance.setNo("No6000654321"); System.out.println("身份证号码为:" + instance.getNo()); } else { System.out.println("重复办理身份证,获取旧号码!"); } return instance; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } } 2. 每一麻将局都有两个骰子,因此骰子就应当是双例类。现使用多例模式模拟该场景。 1) 类图 2) 实现代码: import java.util.Date; import java.util.Random; public class Dice { private static Dice die1 = new Dice(); private static Dice die2 = new Dice(); private Dice() { } public static Dice getInstance(int whichOne) { if (whichOne == 1) { return die1; } else { return die2; } } public synchronized int dice() { Date d = new Date(); Random r = new Random(d.getTime()); int value = r.nextInt(); value = Math.abs(value); value = value % 6; value += 1; return value; } } import java.util.Random; import java.util.Date; public class DiceClient { private static Dice die1, die2; public static void main(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) 实现代码: public interface Discount { public double calculate(double price); } public class MovieTicket { private double price; private Discount discount; //维持一个对抽象折扣类的引用 public void setPrice(double price) { this.price = price; } //注入一个折扣类对象 public void setDiscount(Discount discount) { this.discount = discount; } public double getPrice() { //调用折扣类的折扣价计算方法 return discount.calculate(this.price); } } //VIP会员票折扣类:具体策略类 public class VIPDiscount implements Discount { public double calculate(double price) { System.out.println("VIP票:"); System.out.println("增加积分!"); return price * 0.5; } } //学生票折扣类:具体策略类 public class StudentDiscount implements Discount { public double calculate(double price) { System.out.println("学生票:"); return price * 0.8; } } //儿童票折扣类:具体策略类 public class ChildrenDiscount implements Discount { public double calculate(double price) { System.out.println("儿童票:"); return price - 10; } } public class MoviceClient { public static void main(String args[]) { MovieTicket mt = new MovieTicket(); double originalPrice = 60.0; double currentPrice; mt.setPrice(originalPrice); System.out.println("原始价为:" + originalPrice); System.out.println("---------------------------------"); Discount discount =new VIPDiscount(); //vip 用户 mt.setDiscount(discount); //注入折扣对象 currentPrice = mt.getPrice(); System.out.println("折后价为:" + currentPrice); discount =new StudentDiscount(); //学生用户 mt.setDiscount(discount); //注入折扣对象 currentPrice = mt.getPrice(); System.out.println("折后价为:" + currentPrice); discount =new ChildrenDiscount(); //儿童用户 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) 实现代码: public class plane { private state state;//状态 public void settakeoffFeatures(state tFeatures){ this.state = tFeatures; } public void setplanetype(String type) { if(type=="直升机") state=new Helicopter(); else if(type=="客机"){ state=new AirPlane(); } else if(type=="歼击机"){ state=new Fighter(); } else if(type=="鹞式战斗机"){ state=new Harrier(); } else{ state=null; } } public void takeoff(){ state.takeOff(); } public void fly(){ state.fly(); } } public class AirPlane implements state{ @Override public String takeOff() { System.out.println("长距离起飞"); return "长距离起飞"; } @Override public String fly() { System.out.println("亚音速飞行"); return "亚音速飞行"; } } public class Fighter implements state{ @Override public String takeOff() { System.out.println("长距离起飞"); return "长距离起飞"; } @Override public String fly() { System.out.println("超亚音速飞行"); return "超音速飞行"; } } public class Harrier implements state{ @Override public String takeOff() { System.out.println("垂直起飞"); return "垂直起飞"; } @Override public String fly() { System.out.println("超亚音速飞行"); return "超音速飞行"; } } public class Helicopter implements state{ @Override public String takeOff() { System.out.println("垂直起飞"); return "垂直起飞"; } @Override public String fly() { System.out.println("亚音速飞行"); return "亚音速飞行"; } } public interface state { public String takeOff();//起飞 public String fly();//飞行 } public class Client { public static void main(String args[]) { plane plane = new plane(); plane.setplanetype("歼击机"); plane.takeoff(); plane.fly(); } } 3) 实现结果: 5. 儿子、妈妈、父亲三人合作画一幅画,儿子负责画出一朵花的轮廓,妈妈负责涂上颜色、父亲负责将画裱在画框里。现请使用装饰模式模拟这个过程。  1) 类图 2) 实现代码: public interface painting { public String Draw(); } public class Son implements painting{ @Override public String Draw() { System.out.println("儿子用笔画出了花的轮廓"); return "儿子用笔画出了花的轮廓"; } } public class Father implements painting{ private painting painting;//被装饰者 public Father(painting painting) { this.painting =painting; } private Father() {} public void paint() { //爸爸装饰者做的职责 System.out.println("爸爸正在做上画框前的准备工作"); painting.Draw();//爸爸装饰者做职责 System.out.println("父亲将画裱在画框里"); } @Override public String Draw() { System.out.println("父亲将画裱在画框里"); return "父亲将画裱在画框里"; } } public class Mother implements painting{ private painting painting;//被装饰者 public Mother(painting painting) { this.painting =painting; } private Mother() {} public void paint() { System.out.println("妈妈正在做给画上颜色前的准备工作。"); painting.Draw();//妈妈装饰者做的职责 System.out.println("妈妈给画上好了颜色"); } @Override public String Draw() { System.out.println("妈妈给画上好了颜色"); return "妈妈给画上好了颜色"; } } public class Client { public static void main(String[] args){ painting painting =new Son(); painting.Draw(); painting = new Mother(painting); painting.Draw(); painting = new Father(painting); painting.Draw(); } } 3) 实现结果: 6. 某公司想通过网络传输数据,但是担心文件被窃取。他们的所有数据都采用字符的方式传送。现在他们开发了一个数据加密模块,可以对字符串进行加密,以便数据更安全地传送。最简单的加密算法通过对字母向后移动6位来实现,同时还提供了稍复杂的逆向输出加密,还提供了更为高级的求模加密,让每一位与6求模。 用户先使用最简单的加密算法对字符串进行加密,再对加密之后的结果使用复杂加密算法进行二次加密,再对二次加密结果用高级加密算法进行第三次加密。现请使用装饰模式模拟这个过程。  1) 类图 2) 实现代码: public class ConcreteEncrypt implements EncryptComponet{ private EncryptComponet encryptComponet; public ConcreteEncrypt(EncryptComponet encryptComponet) { super(); this.encryptComponet = encryptComponet; } public void encrypt() { encryptComponet.encrypt(); } } public interface EncryptComponet { public abstract void encrypt(); } public class RawData implements EncryptComponet{ public void encrypt() { System.out.println("这是要发送的数据"); } } public class ReversEncrypt implements EncryptComponet{ public ReversEncrypt(EncryptComponet encryptComponet) { addReservesEncrypt(); } private void addReservesEncrypt() { System.out.println("反向加密"); } @Override public void encrypt() { } } public class SuperEncrypt implements EncryptComponet{ public SuperEncrypt(EncryptComponet encryptComponet) { addSuperEncrypt(); } private void addSuperEncrypt() { System.out.println("最高加密算法"); } @Override public void encrypt() { } } public class TranslateEncrypt implements EncryptComponet{ public TranslateEncrypt(EncryptComponet encryptComponet) { addTranslateEncrypt(); } private void addTranslateEncrypt() { System.out.println("移动加密"); } @Override public void encrypt() { } } public class Client { public static void main(String[] args) { EncryptComponet s0,s1,s2,s3; s0 = new RawData(); s1 = new TranslateEncrypt(s0); s2 = new ReversEncrypt(s1); s3 = new SuperEncrypt(s2); s3.encrypt(); } } 7. 现需要设计一个可以模拟各种动物行为的机器人,在机器人中定义了一系列方法,如机器人叫喊方法cry()、机器人移动方法move()等。如果希望在不修改已有代码的基础上使得机器人能够像狗一样叫,像狗一样跑,使用适配器模式进行系统设计。 1)类图 2) 实现代码 public interface Robot { public void cry(); public void move(); } public class Dog { public void barks(){ System.out.println("狗在叫"); } public void run(){ System.out.println("狗在跑"); } } public class Dogadapter extends Dog implements Robot{ @Override public void cry() { barks(); } @Override public void move() { run(); } } public class Client { public static void main(String[] args) { Dogadapter dogrobot = new Dogadapter(); dogrobot.move(); dogrobot.cry(); } } 3) 实现结果 8. 在NBA作为外籍中锋,我需要翻译,具体场景描述如下:  ² 姚明刚来到NBA,身材够高,球技够好; ² 但是英语不是很懂,听不懂教练的战术安排; ² 球员分为前锋、中锋和后卫; ² 教练会给球员分配进攻、防守任务。 现请使用适配器模式模拟这个场景。 1) 类图 2) 实现代码: public abstract class Player { protected String name; public Player(String name) { this.name = name; } public abstract void attack(); public abstract void defense(); } public class Guards extends Player { public Guards(String name) { super(name); } public void attack() { System.out.println("后卫" + name + "进攻"); } public void defense() { System.out.println("后卫" + name + "防守"); } } public class Center extends Player { public Center(String name) { super(name); } public void attack() { System.out.println("中锋" + name + "进攻"); } public void defense() { System.out.println("中锋" + name + "防守"); } } public class ForeignCenter { private String name; public void attack() { System.out.println("外籍中锋" + name + "进攻"); } public void defense() { System.out.println("外籍中锋" + name + "防守"); } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Forwards extends Player { public Forwards(String name) { super(name); } public void attack() { System.out.println("前锋" + name + "进攻"); } public void defense() { System.out.println("前锋" + name + "防守"); } } public class Translator extends Player { private ForeignCenter wjzf = new ForeignCenter(); public Translator(String name) { super(name); wjzf.setName(name); } public void attack() { wjzf.attack(); } public void defense() { wjzf.defense(); } } public class Client { public static void main(String[] args) { Player b = new Forwards("巴蒂尔"); b.attack(); Player m = new Guards("麦克格雷迪"); m.attack(); Player ym = new Translator("姚明"); ym.attack(); ym.defense(); } } 3) 实现结果: 18
    展开阅读全文
    提示  咨信网温馨提示:
    1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
    2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
    3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
    4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
    5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
    6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

    开通VIP折扣优惠下载文档

    自信AI创作助手
    关于本文
    本文标题:面向对象系统分析和设计综合实验报告4.doc
    链接地址:https://www.zixin.com.cn/doc/3770017.html
    页脚通栏广告

    Copyright ©2010-2026   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


    关注我们 :微信公众号  抖音  微博  LOFTER               

    自信网络  |  ZixinNetwork