设计模式大题参考模板_第1页
设计模式大题参考模板_第2页
设计模式大题参考模板_第3页
设计模式大题参考模板_第4页
设计模式大题参考模板_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、5.2.某电影院售票系统为不同类型的用户提供了不同的电影票(CinemaTicket)打折方式(Discount),学生凭学生证可享受8折优惠(StudentDiscount),儿童可享受减免10元的优惠(ChildrenDiscount), VIP用户除享受半价优惠外还可以进行积分(VIPDiscount)。选择一种合适的设计模式来设计该系统。(策略模式)/电影票类:环境类class MovieTicketprivate double price;private Discount discount;public void setPrice(double price)this.price =

2、price;public void setDiscount(Discount discount)this.discount = discount;public double getPrice()return discount.calculate(this.price);/折扣类:抽象策略类interface Discountpublic double calculate(double price);/学生折扣类:具体策略类class StudentDiscount implements Discount1 / 16public double calculate(double price)ret

3、urn price * 0.8;/儿童折扣类:具体策略类class ChildrenDiscount implements Discountpublic double calculate(double price)return price - 10;/VIP会员折扣类:具体策略类class VIPDiscount implements Discountpublic double calculate(double price)System.out.println("增加积分!");return price * 0.5;/客户端测试类class Clientpublic sta

4、tic void main(String args)MovieTicket mt = new MovieTicket();mt.setPrice(50.00);double currentPrice;Discount obj;obj = new StudentDiscount(); /可通过配置文件实现mt.setDiscount(obj);currentPrice = mt.getPrice();System.out.println("折后价为:" + currentPrice);System.out.println("-");obj = new VI

5、PDiscount();mt.setDiscount(obj);currentPrice = mt.getPrice();System.out.println("折后价为:" + currentPrice);public interface Count public abstract void discount(double a);public class CiTicket private Count disobj;public void discount(double a)disobj.discount(a);public void setdisobj(Count dis

6、obj)this.disobj=disobj;public class StTicket implements Countprivate double a;public void discount(double a)System.out.println("原价为"+a);this.a=a*0.8;System.out.println("学生票的票价为原价的8折:"+this.a);public class VTicket implements Countprivate double a;public void discount(double a)Syst

7、em.out.println("原价为"+a);this.a=a*0.5;System.out.println("VIP会员的票价为原价的半价:"+this.a);System.out.println("VIP会员的会员卡积分一次");public class ChTicket implements Count private double a;public void discount(double a)System.out.println("原价为"+a);this.a=a-10;System.out.print

8、ln("儿童票的票价为原价减10元:"+this.a);public class Client public static void main(String args) CiTicket c=new CiTicket(); Count d = new StTicket(); Count d1 = new ChTicket(); Count d2 = new VTicket(); d.discount(50); d1.discount(50); d2.discount(50); c.setdisobj(d); c.setdisobj(d1); c.setdisobj(d2);

9、 1. 使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请用面向对象的语言实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。import javax.swing.JOptionPane;/* * 主要显示类 * author 陈俊生 * */public class Main /* * 主方法 * param arg */public static void main(String arg)/* * 提示输入 */*System.out.

10、println("请大家输入你想让女娲造的人的代号:");*/String str = ""/* * 当输入0时退出程序 */while(true)str = JOptionPane.showInputDialog("请大家输入你想让女娲造的人的代号:");NvWa nvWa = new NvWa(); Person person = nvWa.madeMan(str);person.play();/* * 男人类 * author 陈俊生 * */class Man extends Person Overridepublic voi

11、d play() / TODO Auto-generated method stub/*System.out.println("大家好,我是男人!我来自地球!");*/JOptionPane.showMessageDialog(null, "大家好,我是男人!我来自地球!");/* * 女娲类 * 简单工厂类 * author 陈俊生 * */class NvWa /* * 女娲造人方法 * param str 根据传递的参数进行造人 * return */public Person madeMan(String str)Person person =

12、null;switch (str) /* * 实例化男人 */case "M":person = new Man();break;/* * 实例化男人 */case "W":person = new Woman();break;/* * 实例化机器人 */case "R":person = new Robot();break;default:/*System.out.println("输入有误,女娲没这个能力造出你需要的人!");*/person = new Person();break;return person

13、;/* * 人类 * author 陈俊生 * */class Person /*private String name;/姓名private int age;/年龄*/* * 玩的方法 */public void play()JOptionPane.showMessageDialog(null, "输入有误,我还是一坨泥巴!女娲还没构造我!请重试");/* * 机器人类 * author 陈俊生 * */class Robot extends Person Overridepublic void play() / TODO Auto-generated method st

14、ub/*System.out.println("大家好,我是机器人!我来自火星!");*/JOptionPane.showMessageDialog(null, "大家好,我是机器人!我来自火星!");/* * 女人类 * author 陈俊生 * */class Woman extends Person Overridepublic void play() / TODO Auto-generated method stub/*System.out.println("大家好,我是女人!我来自水星!");*/JOptionPane.sh

15、owMessageDialog(null, "大家好,我是女人!我来自水星!");2.迟到的理由。(简单工厂和工厂模式)说明:根据迟到的理由不同,如:去厕所,在路上,回家等理由。当老师需要知道是何种理由迟到时只需传入相应的参数,现请使用简单工厂模式来模拟该过程。此外,如果有同学新增加一个“相亲”理由,为了遵循开闭原则,请使用工厂模式模拟上述过程。简单工厂模式import javax.xml.parsers.*;import org.w3c.dom.*;import java.io.*;public class XMLUtilReason /该方法用于从XML配置文件中提取品

16、牌名称,并返回该品牌名称public static String getexcuse()try/创建文档对象DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = dFactory.newDocumentBuilder();Document doc;doc = builder.parse(new File("configReason.xml"); /获取包含品牌名称的文本节点NodeList nl = doc.getElementsByT

17、agName("excuse"); Node classNode=nl.item(0).getFirstChild(); String excuse=classNode.getNodeValue().trim(); return excuse; catch(Exception e) e.printStackTrace(); return null; public abstract class AbstractReason public abstract void reason();public class GoHome extends AbstractReason publ

18、ic void reason() System.out.println("Go Home!");public class GoToilet extends AbstractReason public void reason() System.out.println("Go to Toilet!");public class OnTheWay extends AbstractReason public void reason() System.out.println("On The Way!");public class ReasonF

19、actory public static AbstractReason produceReason (String excuse) throws Exceptionif(excuse.equalsIgnoreCase("Toilet")System.out.println("The ReasonFactory produces the reason of Toilet!");return new GoToilet();else if(excuse.equalsIgnoreCase("Home")System.out.println(&

20、quot;The ReasonFactory produces the reason of go home");return new GoHome();else if(excuse.equalsIgnoreCase("OnTheWay")System.out.println("The ReasonFactory produces the reason of on the way!");return new OnTheWay();else throw new Exception("Sorry! The reason is not tru

21、e!");public class Client_xml public static void main(String args) try AbstractReason abstractreason; String excuse=XMLUtilReason.getexcuse(); abstractreason=ReasonFduceReason(excuse); /abstractreason=ReasonFduceReason ("Home"); abstractreason.reason(); catch(Except

22、ion e) System.out.println(e.getMessage(); public class Client public static void main(String args) try AbstractReason abstractreason; abstractreason=ReasonFduceReason ("Toilet"); abstractreason.reason(); catch(Exception e) System.out.println(e.getMessage(); 3.简历题目(浅克隆)需要有一个简历类,必须

23、要有姓名,可以设置性别和年龄,可以设置工作经历。最终需要三份简历。public class Resume implements Cloneable private String name;private String sex;private String age; private WorkExperience workexperience=null; public Resume(String name)=name;this.workexperience=new WorkExperience();public void setName(String name)

24、=name;public void setPersonalInfo(String sex, String age)this.sex=sex;this.age=age;public String getName()return ();public String getSex()return(this.sex);public String getAge()return(this.age);public void setWorkExperience(String workDate, String Company)workexperience.setworkDate(workDate

25、);workexperience.setCompany(Company);public WorkExperience getWorkExperience()return this.workexperience;public Object clone()Resume clone=null;tryclone=(Resume)super.clone(); catch(CloneNotSupportedException e) System.out.println("Clone failure!"); return clone;public void display() Syste

26、m.out.println("Resume:"+" "+this.getName()+" "+this.getAge()+" "+this.getSex(); System.out.println("Experience:"+" "+workexperience.getworkDate()+" "+workexperience.getCompany();public class WorkExperience private String workDate;

27、private String Company;public void setworkDate(String workDate)this.workDate=workDate;public void setCompany(String Company)this.Company=Company;public String getworkDate()return (this.workDate);public String getCompany()return(this.Company);public class Client public static void main(String args) R

28、esume resume,copy1,copy2; resume=new Resume("zhangsan"); resume.setPersonalInfo("male", "25"); resume.setWorkExperience("1995-1997","Company1"); copy1=(Resume)resume.clone(); copy2=(Resume)resume.clone(); resume.display(); copy1.setName("lisi&qu

29、ot;); copy1.setWorkExperience("2004-2007","Company2"); copy1.display(); copy2.setPersonalInfo("female", "30"); copy2.display(); System.out.println(resume=copy1); System.out.println(copy1=copy2); System.out.println(resume.getWorkExperience()=copy1.getWorkExperi

30、ence(); System.out.println(copy1.getWorkExperience()=copy2.getWorkExperience(); 陈俊生版public class Resume implements Cloneableprivate String name;/姓名private String sex;/性别private int age;/年龄private WorkExperience we ;/工作经历public Resume()this.we = new WorkExperience();/* * 姓名 * return */public String g

31、etName()return name;public void setName(String name) = name;/* * 性别 * return */public String getSex() return sex;public void setSex(String sex) this.sex = sex;/* * 年龄 * return */public int getAge() return age;public void setAge(int age) this.age = age;/* * 获取工作经历对象 */public WorkExperience g

32、etWorkExperience()return we;Overridepublic Object clone()/ TODO Auto-generated method stubResume resume = null;try resume = (Resume)super.clone(); catch (CloneNotSupportedException e) / TODO: handle exceptionJOptionPane.showMessageDialog(null, "克隆失败!");e.printStackTrace();return resume;public class WorkExperience public void setWorkExperience(String str)JOptionPane.showMessageDialog(null, "设置成功n"+str);public static void main(String args) / TODO Auto-generated method stub

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论