面向对象系统分析和设计综合实验报告.doc
实验名称:实验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("No");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 stateOverridepublic String takeOff() System.out.println("长距离起飞");return "长距离起飞"Overridepublic String fly() System.out.println("亚音速飞行");return "亚音速飞行"public class Fighter implements stateOverridepublic String takeOff() System.out.println("长距离起飞");return "长距离起飞"Overridepublic String fly() System.out.println("超亚音速飞行");return "超音速飞行"public class Harrier implements stateOverridepublic String takeOff() System.out.println("垂直起飞");return "垂直起飞"Overridepublic String fly() System.out.println("超亚音速飞行");return "超音速飞行"public class Helicopter implements stateOverridepublic String takeOff() System.out.println("垂直起飞");return "垂直起飞"Overridepublic 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 paintingOverridepublic 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("父亲将画裱在画框里"); Overridepublic String Draw() System.out.println("父亲将画裱在画框里");return "父亲将画裱在画框里"public class Mother implements paintingprivate painting painting;/被装饰者public Mother(painting painting) this.painting =painting;private Mother() public void paint() System.out.println("妈妈正在做给画上颜色前的准备工作。");painting.Draw();/妈妈装饰者做的职责System.out.println("妈妈给画上好了颜色");Overridepublic 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 EncryptComponetprivate 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 EncryptComponetpublic void encrypt() System.out.println("这是要发送的数据"); public class ReversEncrypt implements EncryptComponetpublic ReversEncrypt(EncryptComponet encryptComponet) addReservesEncrypt(); private void addReservesEncrypt() System.out.println("反向加密"); Overridepublic void encrypt() public class SuperEncrypt implements EncryptComponetpublic SuperEncrypt(EncryptComponet encryptComponet) addSuperEncrypt(); private void addSuperEncrypt() System.out.println("最高加密算法"); Overridepublic void encrypt() public class TranslateEncrypt implements EncryptComponetpublic TranslateEncrypt(EncryptComponet encryptComponet) addTranslateEncrypt(); private void addTranslateEncrypt() System.out.println("移动加密"); Overridepublic 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 RobotOverridepublic void cry() barks();Overridepublic 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) 实现结果: