您好,欢迎访问三七文档
5.2.某电影院售票系统为不同类型的用户提供了不同的电影票(CinemaTicket)打折方式(Discount),学生凭学生证可享受8折优惠(StudentDiscount),儿童可享受减免10元的优惠(ChildrenDiscount),VIP用户除享受半价优惠外还可以进行积分(VIPDiscount)。选择一种合适的设计模式来设计该系统。(策略模式)StudentDiscount+calculate(doubleprice)...:doubleDiscount+calculate(doubleprice)...:doubleMovieTicket--pricediscount:double:Discount+++setPrice(doubleprice)setDiscount(Discountdiscount)getPrice()...:void:void:doubleChildrenDiscount+calculate(doubleprice)...:doubleVIPDiscount+calculate(doubleprice)...:double//电影票类:环境类classMovieTicket{privatedoubleprice;privateDiscountdiscount;publicvoidsetPrice(doubleprice){this.price=price;}publicvoidsetDiscount(Discountdiscount){this.discount=discount;}publicdoublegetPrice(){returndiscount.calculate(this.price);}}//折扣类:抽象策略类interfaceDiscount{publicdoublecalculate(doubleprice);}//学生折扣类:具体策略类classStudentDiscountimplementsDiscount{publicdoublecalculate(doubleprice){returnprice*0.8;}}//儿童折扣类:具体策略类classChildrenDiscountimplementsDiscount{publicdoublecalculate(doubleprice){returnprice-10;}}//VIP会员折扣类:具体策略类classVIPDiscountimplementsDiscount{publicdoublecalculate(doubleprice){System.out.println(增加积分!);returnprice*0.5;}}//客户端测试类classClient{publicstaticvoidmain(Stringargs[]){MovieTicketmt=newMovieTicket();mt.setPrice(50.00);doublecurrentPrice;Discountobj;obj=newStudentDiscount();//可通过配置文件实现mt.setDiscount(obj);currentPrice=mt.getPrice();System.out.println(折后价为:+currentPrice);System.out.println(---------------------------------);obj=newVIPDiscount();mt.setDiscount(obj);currentPrice=mt.getPrice();System.out.println(折后价为:+currentPrice);}}publicinterfaceCount{publicabstractvoiddiscount(doublea);}publicclassCiTicket{privateCountdisobj;publicvoiddiscount(doublea){disobj.discount(a);}publicvoidsetdisobj(Countdisobj){this.disobj=disobj;}}publicclassStTicketimplementsCount{privatedoublea;publicvoiddiscount(doublea){System.out.println(原价为+a);this.a=a*0.8;System.out.println(学生票的票价为原价的8折:+this.a);}}publicclassVTicketimplementsCount{privatedoublea;publicvoiddiscount(doublea){System.out.println(原价为+a);this.a=a*0.5;System.out.println(VIP会员的票价为原价的半价:+this.a);System.out.println(VIP会员的会员卡积分一次);}}publicclassChTicketimplementsCount{privatedoublea;publicvoiddiscount(doublea){System.out.println(原价为+a);this.a=a-10;System.out.println(儿童票的票价为原价减10元:+this.a);}}publicclassClient{publicstaticvoidmain(String[]args){CiTicketc=newCiTicket();Countd=newStTicket();Countd1=newChTicket();Countd2=newVTicket();d.discount(50);d1.discount(50);d2.discount(50);c.setdisobj(d);c.setdisobj(d1);c.setdisobj(d2);}}1.使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请用面向对象的语言实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。importjavax.swing.JOptionPane;/***主要显示类*@author陈俊生**/publicclassMain{/***主方法*@paramarg*/publicstaticvoidmain(Stringarg[]){/**提示输入*//*System.out.println(请大家输入你想让女娲造的人的代号:);*/Stringstr=;/**当输入0时退出程序*/while(true){str=JOptionPane.showInputDialog(请大家输入你想让女娲造的人的代号:);NvWanvWa=newNvWa();Personperson=nvWa.madeMan(str);person.play();}}}/***男人类*@author陈俊生**/classManextendsPerson{@Overridepublicvoidplay(){//TODOAuto-generatedmethodstub/*System.out.println(大家好,我是男人!我来自地球!);*/JOptionPane.showMessageDialog(null,大家好,我是男人!我来自地球!);}}/***女娲类*简单工厂类*@author陈俊生**/classNvWa{/***女娲造人方法*@paramstr根据传递的参数进行造人*@return*/publicPersonmadeMan(Stringstr){Personperson=null;switch(str){/**实例化男人*/caseM:person=newMan();break;/**实例化男人*/caseW:person=newWoman();break;/**实例化机器人*/caseR:person=newRobot();break;default:/*System.out.println(输入有误,女娲没这个能力造出你需要的人!);*/person=newPerson();break;}returnperson;}}/***人类*@author陈俊生**/classPerson{/*privateStringname;//姓名privateintage;//年龄*//***玩的方法*/publicvoidplay(){JOptionPane.showMessageDialog(null,输入有误,我还是一坨泥巴!女娲还没构造我!请重试~);};}/***机器人类*@author陈俊生**/classRobotextendsPerson{@Overridepublicvoidplay(){//TODOAuto-generatedmethodstub/*System.out.println(大家好,我是机器人!我来自火星!);*/JOptionPane.showMessageDialog(null,大家好,我是机器人!我来自火星!);}}/***女人类*@author陈俊生**/classWomanextendsPerson{@Overridepublicvoidplay(){//TODOAuto-generatedmethodstub/*System.out.println(大家好,我是女人!我来自水星!);*/JOptionPane.showMessageDialog(null,大家好,我是女人!我来自水星!);}}2.迟到的理由。(简单工厂和工厂模式)说明:根据迟到的理由不同,如:去厕所,在路上,回家等理由。当老师需要知道是何种理由迟到时只需传入相应的参数,现请使用简单工厂模式来模拟该过程。此外,如果有同学新增加一个“相亲”理由,为了遵循开闭原则,请使用工厂模式模拟上述过程。简单工厂模式importjavax.xml.parsers.*;importorg.w3c.dom.*;importjava.io.*;publicclassXMLUtilReason{//该方法用于从XML配置文件中提取品牌名称,并返回该品牌名称publicstaticStringgetexcuse(){try{//创建文档对象DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dFactory.newDocumentBuilder();Documentdoc;doc=builder.parse(newFile(configReason.xml));//获取包含品牌名称的文本节点NodeListnl=doc.getElementsByTagName(excuse);NodeclassNode=nl.item(0).getFirstChild();Stringexcuse=classNode.getNodeValue().trim();returnexcuse;}catch(Exceptione){e.printStackTrace();returnnull;}}}publicabstractclassAbstractReason{publicabstractvoidreason();}publicclassGoHomeextendsAbstractReason{publicvoidreason(){System.out.println(GoHome!);}}publicclassGoToiletextendsAbstractReason{publicvoidreason(){System.out.println(G
本文标题:设计模式大题
链接地址:https://www.777doc.com/doc-7199037 .html