《设计模式大题(共16页).doc》由会员分享,可在线阅读,更多相关《设计模式大题(共16页).doc(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上5.2.某电影院售票系统为不同类型的用户提供了不同的电影票(CinemaTicket)打折方式(Discount),学生凭学生证可享受8折优惠(StudentDiscount),儿童可享受减免10元的优惠(ChildrenDiscount), VIP用户除享受半价优惠外还可以进行积分(VIPDiscount)。选择一种合适的设计模式来设计该系统。(策略模式)/电影票类:环境类class MovieTicketprivate double price;private Discount discount;public void setPrice(double price)
2、this.price = 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 Discountpublic double calculate(double pr
3、ice)return 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 static v
4、oid 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 VIPDiscount();mt.setDiscount(ob
5、j);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 disobj)this.disobj=disobj;public class StTic
6、ket 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)System.out.println(原价为+a);this.a=a*0.5;System.out.println(VIP会员的票价为原价
7、的半价:+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.println(儿童票的票价为原价减10元:+this.a);public class Client public static void main(String args) CiTicket c=new CiTicket(); Cou
8、nt 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); 1. 使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请用面向对象的语言实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,
9、对代码进行修改并注意女娲的变化。import javax.swing.JOptionPane;/* * 主要显示类 * author 陈俊生 * */public class Main /* * 主方法 * param arg */public static void main(String arg)/* * 提示输入 */*System.out.println(请大家输入你想让女娲造的人的代号:);*/String str = ;/* * 当输入0时退出程序 */while(true)str = JOptionPane.showInputDialog(请大家输入你想让女娲造的人的代号:);N
10、vWa nvWa = new NvWa(); Person person = nvWa.madeMan(str);person.play();/* * 男人类 * author 陈俊生 * */class Man extends Person Overridepublic void play() / TODO Auto-generated method stub/*System.out.println(大家好,我是男人!我来自地球!);*/JOptionPane.showMessageDialog(null, 大家好,我是男人!我来自地球!);/* * 女娲类 * 简单工厂类 * author
11、 陈俊生 * */class NvWa /* * 女娲造人方法 * param str 根据传递的参数进行造人 * return */public Person madeMan(String str)Person person = null;switch (str) /* * 实例化男人 */case M:person = new Man();break;/* * 实例化男人 */case W:person = new Woman();break;/* * 实例化机器人 */case R:person = new Robot();break;default:/*System.out.print
12、ln(输入有误,女娲没这个能力造出你需要的人!);*/person = new Person();break;return person;/* * 人类 * author 陈俊生 * */class Person /*private String name;/姓名private int age;/年龄*/* * 玩的方法 */public void play()JOptionPane.showMessageDialog(null, 输入有误,我还是一坨泥巴!女娲还没构造我!请重试);/* * 机器人类 * author 陈俊生 * */class Robot extends Person Ov
13、erridepublic void play() / TODO Auto-generated method stub/*System.out.println(大家好,我是机器人!我来自火星!);*/JOptionPane.showMessageDialog(null, 大家好,我是机器人!我来自火星!);/* * 女人类 * author 陈俊生 * */class Woman extends Person Overridepublic void play() / TODO Auto-generated method stub/*System.out.println(大家好,我是女人!我来自水
14、星!);*/JOptionPane.showMessageDialog(null, 大家好,我是女人!我来自水星!);2.迟到的理由。(简单工厂和工厂模式)说明:根据迟到的理由不同,如:去厕所,在路上,回家等理由。当老师需要知道是何种理由迟到时只需传入相应的参数,现请使用简单工厂模式来模拟该过程。此外,如果有同学新增加一个“相亲”理由,为了遵循开闭原则,请使用工厂模式模拟上述过程。简单工厂模式import javax.xml.parsers.*;import org.w3c.dom.*;import java.io.*;public class XMLUtilReason /该方法用于从XML
15、配置文件中提取品牌名称,并返回该品牌名称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.getElementsByTagNa
16、me(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 public void reason()
17、 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 ReasonFactory public static AbstractReason produceReason (S
18、tring 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(The ReasonFactory produces the reason of go home);return new GoHome();else if(excuse.equa
19、lsIgnoreCase(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 true!);public class Client_xml public static void main(String args) try AbstractReason abstractreason; String excuse=XMLUtilReason.ge
20、texcuse(); abstractreason=ReasonFactory.produceReason(excuse); /abstractreason=ReasonFactory.produceReason (Home); abstractreason.reason(); catch(Exception e) System.out.println(e.getMessage(); public class Client public static void main(String args) try AbstractReason abstractreason; abstractreason
21、=ReasonFactory.produceReason (Toilet); abstractreason.reason(); catch(Exception e) System.out.println(e.getMessage(); 3.简历题目(浅克隆)需要有一个简历类,必须要有姓名,可以设置性别和年龄,可以设置工作经历。最终需要三份简历。public class Resume implements Cloneable private String name;private String sex;private String age; private WorkExperience work
22、experience=null; public Resume(String name)this.name=name;this.workexperience=new WorkExperience();public void setName(String name)this.name=name;public void setPersonalInfo(String sex, String age)this.sex=sex;this.age=age;public String getName()return (this.name);public String getSex()return(this.s
23、ex);public String getAge()return(this.age);public void setWorkExperience(String workDate, String Company)workexperience.setworkDate(workDate);workexperience.setCompany(Company);public WorkExperience getWorkExperience()return this.workexperience;public Object clone()Resume clone=null;tryclone=(Resume
24、)super.clone(); catch(CloneNotSupportedException e) System.out.println(Clone failure!); return clone;public void display() System.out.println(Resume:+ +this.getName()+ +this.getAge()+ +this.getSex(); System.out.println(Experience:+ +workexperience.getworkDate()+ +workexperience.getCompany();public c
25、lass WorkExperience private String workDate;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 C
26、lient public static void main(String args) Resume 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); copy1.setWorkExperienc
27、e(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.getWorkExperience(); System.out.println(copy1.getWorkExperience()=copy2.getWorkExperience
28、(); 陈俊生版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 getName()return name;public void setName(String name)this.name = name;/* * 性别
29、 * 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 getWorkExperience()return we;Overridepublic Object clone()/ TODO Auto-generat
30、ed 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.showMessag
31、eDialog(null, 设置成功n+str);public static void main(String args) / TODO Auto-generated method stubResume resume , cr1 ,cr2 ,cr3;WorkExperience we1, we2, we3;resume = new Resume();cr1 = (Resume)resume.clone();cr1.setName(张三);String name1 = cr1.getName();cr1.setAge(15);int age1 = cr1.getAge();cr1.setSex(
32、男);String sex1 = cr1.getSex();we1 = cr1.getWorkExperience();we1.setWorkExperience(我叫+ name1 + ,我+age1+ 岁 +,我是+sex1+生,是一名java程序猿!);cr2 = (Resume)resume.clone();cr2.setName(李四);String name2 = cr2.getName();cr2.setAge(20);int age2 = cr2.getAge();cr2.setSex(女);String sex2 = cr2.getSex();we2 = cr2.getWor
33、kExperience();we1.setWorkExperience(我叫+ name2 + 我+age2+ 岁 +我是+sex2+生,是一名美工!);cr3 = (Resume)resume.clone();cr3.setName(王五);String name3 = cr3.getName();cr3.setAge(30);int age3 = cr1.getAge();cr3.setSex(中性人);String sex3 = cr1.getSex();we3 = cr3.getWorkExperience();we1.setWorkExperience(我叫+ name3 + 我+age3+ 岁 +我是+sex3+生,是一名软件测试工程师!);专心-专注-专业