欢迎来到天天文库
浏览记录
ID:35342805
大小:66.14 KB
页数:6页
时间:2019-03-23
《实验九观察者模式、策略模式》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验九淘宝店号530213一、实验目的1、熟练掌握观察值模式、策略模式的建模方式2、掌握如何应用观察值模式、策略模式编程解决具体问题二、实验器材1、计算机一台2、JCreator软件实验内容1、以红绿灯为例,用观察值模式模拟实现现实生活屮行人、车辆以及红绿灯Z间的关系2、设计一个网上书店,该系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,所有语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元有10元的折扣。现使用策略模式來设计该系统,绘制类图并编程实现。三、实验结果程序代码:(1)观察者模式代
2、码packagecom.zfs.pojo;publicclassClient{publicstaticvoidmain(String[]args){TrafficLightroad=newRoadLight();TrafficLightwalk=newWalkerLight();TrafficObservewalker=newWalker();TrafficObservevehicle=newVehicle();road.attach(vehicle);walk.attach(walker);M);road.changeColor();walk.changeColor
3、();System.out.println(H-road.changeColor();walk.changeColor();packagecom.zfs.pojo;importjava.awt.Color;publicclassRoadLightextendsTrafficLight{publicRoadLight(){this.color=Hredn;1@OverridepublicvoidchangeColor(){if(this.color.equals(',red,,)){this.color="green”;}else{this.color=HredM;}f
4、or(Objectobs:observers){((TrafficObserve)obs).response(this.color);}packagecom.zfs.pojo;importjava.awt.Color;importjava.util.ArrayList;publicabstractclassTrafficLight{protectedStringcolor;newprotectedArrayListobserversArrayList();publicabstractvoidchangeC
5、olor();publicvoidattach(TrafficObserveobs){observers.add(obs);publicvoidremove(TrafficObserveobs){observers.remove(obs);}}packagecom.zfs.pojo;publicinterfaceTrafficObserve{publicvoidresponse(Stringcolor);}packagecom.zfs.pojo;publicclassVehicleimplementsTrafficObserve)@Overridepublicvoid
6、response(Stringcolor){if(color.equals("redH)){System.out.printlnC'车灯变红,车辆请等待”);}else{System.out.printlnC'车灯变绿,车辆可以通行”);}}packagecom.zfs.pojo;publicclassWalkerimplementsTrafficObserve{@Overridepublicvoidresponse(Stringcolor){if(color.equals("redu)){System.out.printlnC'行人灯变红,行人请等待”);}else
7、{System.out.printlnC'行人灯变绿,彳亍人可以通行”);}}}packagecom.zfs.pojo;publicclassWalkerLightextendsTrafficLight{publicWalkerLight(){this.color=Hgreenu;@OverridepublicvoidchangeColor(){if(this.color.equals(nred")){this.color=!,greenH;}else{this.color=HredM;for(Objectobs:observers){((Traff
此文档下载收益归作者所有