1、(完整word)设计模式-软件体系结构-实验6-中南大学-软件学院软件体系结构实验报告项目名称 行为型设计模式实验 专业班级 学 号 姓 名 实验成绩:批阅教师:年 月 日22实验6 行为型设计模式实验实验学时: 2 每组人数: 1 实验类型: 3 (1:基础性 2:综合性 3:设计性 4:研究性)实验要求: 1 (1:必修 2:选修 3:其它)实验类别: 3 (1:基础 2:专业基础 3:专业 4:其它)一、实验目的熟练使用PowerDesigner和任意一种面向对象编程语言实现几种常见的行为型设计模式,包括观察者模式和策略模式,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实
2、现这些模式。二、实验内容1. 某实时在线股票软件需要提供如下功能:当股票购买者所购买的某支股票价格变化幅度达到5时,系统将自动发送通知(包括新价格)给购买该股票的所有股民。试使用观察者模式设计并实现该系统,要求绘制相应的类图并编程模拟实现。2. 某公司欲开发一套机房监控系统,如果机房达到某一指定温度,温度传感器(Thermosensor)将自动传递信号给各种响应设备,例如警示灯(CautionLight)将闪烁(flicker())、报警器(Annunciator)将发出警报(alarm())、安全逃生门(SecurityDoor)将自动开启(open()、隔热门(InsulatedDoor)
3、将自动关闭(close()等,每一种响应设备的行为由专门的程序来控制。为了将来能够方便地引入新类型的响应设备,采用观察者模式和适配器模式设计并模拟实现该系统。3. 某软件公司为某电影院开发了一套影院售票系统,在该系统中需要为不同类型的用户提供不同的电影票(MovieTicket)打折(Discount)方式,具体打折方案如下:(1) 学生凭学生证可享受票价8折优惠;(2) 年龄在10周岁及以下的儿童可享受每张票减免10元的优惠(原始票价需大于等于20元);(3) 影院VIP用户除享受票价半价优惠外还可进行积分,积分累计到一定额度可换取电影院赠送的奖品。该系统在将来可能还要根据需要引入新的打折方
4、式。试使用策略模式设计并编程模拟实现该影院售票系统。4。 某软件公司欲开发一款飞机模拟系统,该系统主要模拟不同种类飞机的飞行特征与起飞特征,需要模拟的飞机种类及其特征如表1所示:表1 飞机种类及特征一览表飞机种类起飞特征飞行特征直升机(Helicopter)垂直起飞(VerticalTakeOff)亚音速飞行(SubSonicFly)客机(AirPlane)长距离起飞(LongDistanceTakeOff)亚音速飞行(SubSonicFly)歼击机(Fighter)长距离起飞(LongDistanceTakeOff)超音速飞行(SuperSonicFly)鹞式战斗机(Harrier)垂直起飞
5、(VerticalTakeOff)超音速飞行(SuperSonicFly)为将来能够模拟更多种类的飞机,试采用策略模式设计并模拟实现该飞机模拟系统。三、实验要求1。 结合实例,正确无误地绘制观察者模式和策略模式的模式结构图;2. 使用任意一种面向对象编程语言实现观察者模式和策略模式实例,代码运行正确无误。四、实验步骤1。 结合实例,使用PowerDesigner绘制观察者模式实例结构图并用面向对象编程语言实现该模式实例;2. 结合实例,使用PowerDesigner绘制观察者模式实例结构图并用面向对象编程语言实现该模式实例;3。 结合实例,使用PowerDesigner绘制策略模式实例结构图并
6、用面向对象编程语言实现该模式实例;4. 结合实例,使用PowerDesigner绘制策略模式实例结构图并用面向对象编程语言实现该模式实例。五、实验结果1. 类图:实现代码:/代码颜色使用黑色,字体使用Times New Roman或Arial,字号为五号,如abstract class Shapeimport util。XMLUtil;public class Client public static void main(String args) StockSubject subject = (StockSubject) XMLUtil.getBean(”subject);InvesterOb
7、server observer = (InvesterObserver) XMLUtil。getBean(observer”);subject.attach(observer);subject.notice();public class Invester implements InvesterObserver Overridepublic void update() System.out。println(”今年股票风云莫测,这都是命啊!);public class Stock extends StockSubject public void notice() for (InvesterObse
8、rver invester : super。observerList) invester.update(); public interface InvesterObserver void update();import java.util。ArrayList;public abstract class StockSubject protected ArrayListInvesterObserver observerList = new ArrayListInvesterObserver();public void attach(InvesterObserver observer) observ
9、erList。add(observer);public void detach(InvesterObserver observer) observerList。remove(observer);public abstract void notice();package util;import java.io。File;import java。io。IOException;import javax。xml。parsers。DocumentBuilder;import javax.xml。parsers.DocumentBuilderFactory;import javax.xml.parsers
10、.ParserConfigurationException;import org.w3c.dom.Document;import org。w3c。dom。Node;import org.w3c。dom.NodeList;import org。xml。sax.SAXException;public class XMLUtil public static Object getBean (String tagName) try /创建DOM文档对象DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();Docu
11、mentBuilder docBuilder = docFactory.newDocumentBuilder();Document document = docBuilder。parse(new File(config.xml));/获取包含类名的 文本节点NodeList nl = document。getElementsByTagName(tagName);Node classNode = nl.item(0)。getFirstChild();String className = classNode。getNodeValue();/通过类名生成实例对象并将其返回SuppressWarnin
12、gs(rawtypes”)Class clazz = Class.forName(className);Object obj = clazz.newInstance();return obj; catch (ParserConfigurationException e) e。printStackTrace(); catch (SAXException e) e。printStackTrace(); catch (IOException e) e。printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace(); ca
13、tch (InstantiationException e) e。printStackTrace(); catch (IllegalAccessException e) e.printStackTrace(); return null;?xml version=1。0 encoding=”UTF-8”?xmlbodysubjectStockInvester/observer2。 类图:实现代码:public class Client public static void main(String args) MySubject subject = new Thermosensor();MyObs
14、erver cautionLight = new CautionLightAdapter();MyObserver annunciator = new AnnunciatorAdapter();MyObserver securityDoor = new SecurityDoorAdapter();MyObserver insulatedDoor = new InsulatedDoorAdapter();subject.attach(cautionLight);subject.attach(annunciator);subject。attach(securityDoor);subject.att
15、ach(insulatedDoor);subject。warning();import java。util。ArrayList;public abstract class MySubject protected ArrayListMyObserver observerList = new ArrayList();public void attach(MyObserver myObserver) observerList。add(myObserver);public void detach(MyObserver myObserver) observerList。remove(myObserver
16、);public abstract void warning();public interface MyObserver void update();public class Thermosensor extends MySubject public void warning() System。out.println(”温度达到警报界限”); for (MyObserver observer : super。observerList) observer。update(); public class AnnunciatorAdapter implements MyObserver public
17、Annunciator annunciator = new Annunciator();Overridepublic void update() annunciator。alarm();public class Annunciator public void alarm() System.out。println(报警器发出警报);public class CautionLightAdapter implements MyObserver public CautionLight cautionLight = new CautionLight();Overridepublic void updat
18、e() cautionLight。flicker();public class CautionLight public void flicker() System.out。println(警示灯将闪烁”);public class InsulatedDoorAdapter implements MyObserver public InsulatedDoor insulatedDoor = new InsulatedDoor();Overridepublic void update() insulatedDoor.close();public class InsulatedDoor public
19、 void close() System.out.println(”隔热门自动关闭”);public class SecurityDoorAdapter implements MyObserver public SecurityDoor securityDoor = new SecurityDoor();Overridepublic void update() securityDoor。open();public class SecurityDoor public void open() System。out。println(安全逃生门自动开启);3. 类图:实现代码:import util.
20、XMLUtil;public class Client public static void main(String args) TicketingSystem ticketingSystem = new TicketingSystem();MovieTicket ticket = (MovieTicket) XMLUtil。getBean();ticketingSystem。setTicketKiind(ticket);ticketingSystem.sale();public interface MovieTicket void sale();public class TicketingS
21、ystem private MovieTicket movieTicket;public void setTicketKiind(MovieTicket movieTicket) this。movieTicket = movieTicket;public void sale() movieTicket。sale();public class ChildTicket implements MovieTicket public void sale() System。out。println(”享受每张票减免10元的优惠);public class StudentTicket implements M
22、ovieTicket public void sale() System。out.println(享受票价8折优惠);public class VIPTicket implements MovieTicket public void sale() System.out。println(享受票价半价优惠外还可进行积分”);package util;import java。io。File;import java。io。IOException;import javax。xml。parsers。DocumentBuilder;import javax.xml。parsers.DocumentBuild
23、erFactory;import javax.xml.parsers。ParserConfigurationException;import org.w3c.dom。Document;import org.w3c。dom。Node;import org。w3c.dom。NodeList;import org。xml.sax。SAXException;public class XMLUtil public static Object getBean () try /创建DOM文档对象DocumentBuilderFactory docFactory = DocumentBuilderFactor
24、y.newInstance();DocumentBuilder docBuilder = docFactory。newDocumentBuilder();Document document = docBuilder.parse(new File(config。xml”));/获取包含类名的 文本节点NodeList nl = document.getElementsByTagName(”className);Node classNode = nl.item(0)。getFirstChild();String className = classNode。getNodeValue();/通过类名生
25、成实例对象并将其返回SuppressWarnings(”rawtypes”)Class clazz = Class。forName(className);Object obj = clazz.newInstance();return obj; catch (ParserConfigurationException e) e。printStackTrace(); catch (SAXException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); catch (ClassNotFoundException e
26、) e。printStackTrace(); catch (InstantiationException e) e.printStackTrace(); catch (IllegalAccessException e) e。printStackTrace(); return null;?xml version=1。0 encoding=”UTF8?StudentTicket/className/xmlbody4。 类图:实现代码:import util。XMLUtil;public class Client public static void main(String args) TakeOf
27、f takeOff = (TakeOff) XMLUtil.getBean(”takeOff);Fly fly = (Fly) XMLUtil。getBean(”fly);Aircraft aircraft = new Aircraft(”直升机”, takeOff, fly);System.out。println(”今天是国庆,接下下来是” + aircraft。getKind() + 方阵”);aircraft.takeOff();aircraft。fly();public class Aircraft private String kind;private TakeOff takeOff
28、;private Fly fly;public Aircraft(String kind, TakeOff takeOff, Fly fly) this。kind = kind;this。takeOff = takeOff;this。fly = fly;public void takeOff() takeOff.takeOff();public void fly() fly。fly();public String getKind () return kind;public abstract class Fly public void fly() public abstract class Ta
29、keOff public void takeOff() public class LongDistanceTakeOff extends TakeOff public void takeOff() System。out。println(长距离起飞); public class VerticalTakeOff extends TakeOff Overridepublic void takeOff() System。out.println(”垂直起飞);public class SubSonicFly extends Fly public void fly() System.out。println(亚音速飞行”); public class SuperSonicFly extends Fly public void fly() System.out。println(”超音速飞行”); 六、实验小结本次实验过程中学习了多个行为型模式,结合之前学到的创建型模式以及结构型模式,对整个设计模式有了更深刻的了解了.行为型模式最主要是用来对责任的划分和算法的抽象化。