設(shè)計(jì)模式大題_第1頁
設(shè)計(jì)模式大題_第2頁
設(shè)計(jì)模式大題_第3頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、52某電影院售票系統(tǒng)為不同類型的用戶提供了不同的電影票(Ci nemaTicket)打折方式(Discount),學(xué)生憑學(xué)生證可享受 8折優(yōu)惠(StudentDiscount),兒童可享受減免 10元的優(yōu)惠 (ChildrenDiscount), VIP用戶除享受半價(jià)優(yōu)惠外還可以進(jìn)行積分 (VIPDiscount)。選擇一種合 適的設(shè)計(jì)模式來設(shè)計(jì)該系統(tǒng)。(策略模式)/電影票類:環(huán)境類class MovieTicketprivate double price;private Disco unt disco unt;public void setPrice(double price)this.pr

2、ice = price;public void setDisco un t(Disco unt disco unt)this.disco unt = disco unt;public double getPrice()retur n disco un t.calculate(this.price);/折扣類:抽象策略類in terface Disco untpublic double calculate(double price); /學(xué)生折扣類:具體策略類class Stude ntDisco unt impleme nts Disco unt public double calculate

3、(double price)return price * 0.8;/ 兒童折扣類:具體策略類class ChildrenDiscount implements Discountpublic double calculate(double price)return price - 10;/VIP 會(huì)員折扣類:具體策略類class VIPDiscount implements Discountpublic double calculate(double price)(” 增加積分!”);return price * 0.5;/ 客戶端測試類class Clientpublic static voi

4、d main(String args)MovieTicket mt = new MovieTicket(); mt.setPrice(50.00); double currentPrice;Discount obj;obj = new StudentDiscount(); / 可通過配置文件實(shí)現(xiàn) mt.setDiscount(obj);currentPrice = mt.getPrice();(” 折后價(jià)為:” + curren tPrice);;obj = new VIPDiscount();mt.setDiscount(obj);currentPrice = mt.getPrice();(

5、” 折后價(jià)為:” + curren tPrice);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 StTicket implements Count private double a;public

6、void discount(double a)(”原價(jià)為"+a);this.a=a*0.8;(”學(xué)生票的票價(jià)為原價(jià)的8折:"+this.a);public class VTicket implements Count private double a;public void discount(double a)(”原價(jià)為"+a); this.a=a*0.5;會(huì)員的票價(jià)為原價(jià)的半價(jià) :"+this.a);會(huì)員的會(huì)員卡積分一次 ");public class ChTicket impleme nts Count private double a;p

7、ublic void disco un t(double a)原價(jià)為"+a);this.a=a-10;兒童票的票價(jià)為原價(jià)減10元:"+this.a);public class Clie nt public static void main( Stri ng args) CiTicket c=new CiTicket();Cou nt d = new StTicket();Cou nt d1 = new ChTicket();Cou nt d2 = new VTicket();d.discou nt(50);d1.discou nt(50);d2.discou nt(50);

8、c.setdisobj(d);c.setdisobj(d1);c.setdisobj(d2);1. 使用簡單工廠模式模擬女媧(Nvwa)造人(Person),如果傳入?yún)?shù) M ,則返回一個(gè) Man對(duì)象,如果傳入?yún)?shù) W,則返回一個(gè) Woman對(duì)象,請(qǐng)用面向?qū)ο蟮恼Z言實(shí)現(xiàn)該場景?,F(xiàn)需要增加 一個(gè)新的Robot類,如果傳入?yún)?shù)R,則返回一個(gè)Robot對(duì)象,對(duì)代碼進(jìn)行修改并注意女媧的變化。import javax.swi ng.JOptio nPane;*主要顯示類* author陳俊生*/public class Mai n *主方法* param arg*/public static void

9、main(String arg)/* 提示輸入*/請(qǐng)大家輸入你想讓女媧造的人的代號(hào): ");*/String str = ""/* 當(dāng)輸入 0 時(shí)退出程序*/while(true)str = JOptionPane.showInputDialog(" 請(qǐng)大家輸入你想讓女媧造的人的代號(hào):");NvWa nvWa = new NvWa();Person person = nvWa.madeMan(str);person.play();/*陳俊生* 男人類* author*/ class Man extends Person Override pub

10、lic void play() / TODO Auto-generated method stub大家好,我是男人!我來自地球! ");*/ JOptionPane.showMessageDialog(null, "大家好,我是男人!我來自地球! "); * 女媧類* 簡單工廠類* author 陳俊生*/class NvWa * 女媧造人方法* param str 根據(jù)傳遞的參數(shù)進(jìn)行造人* return*/public Person madeMan(String str)Person person = null;switch (str) /* 實(shí)例化男人*/ca

11、se "M":person = new Man(); break;/* 實(shí)例化男人*/case "W":person = new Woman(); break;/* 實(shí)例化機(jī)器人*/case "R":person = new Robot(); break;default:/*輸入有誤,女媧沒這個(gè)能力造出你需要的人!");*/person = new Person();break;return person;/* 人類* author 陳俊生*/ class Person /* private String name;/ 姓名

12、 private int age;/ 年齡*/* 玩的方法*/ public void play()JOptionPane.showMessageDialog(null, "輸入有誤, 我還是一坨泥巴! 女媧還沒構(gòu)造我! 請(qǐng)重試 ");* 機(jī)器人類* author 陳俊生*/class Robot extends Person Override public void play() / TODO Auto-generated method stub大家好,我是機(jī)器人!我來自火星! ");*/ JOptionPane.showMessageDialog(null,

13、"大家好,我是機(jī)器人!我來自火星!");* 女人類* author 陳俊生*/class Woman extends Person Override public void play() / TODO Auto-generated method stub大家好,我是女人!我來自水星! ");*/ JOptionPane.showMessageDialog(null, "大家好,我是女人!我來自水星! "); 2. 遲到的理由。 (簡單工廠和工廠模式) 說明:根據(jù)遲到的理由不同,如:去廁所,在路上,回家等理由。當(dāng)老師需要知道是何種理 由遲到時(shí)只需

14、傳入相應(yīng)的參數(shù),現(xiàn)請(qǐng)使用簡單工廠模式來模擬該過程。此外,如果有同學(xué)新增加一個(gè)“相親”理由,為了遵循開閉原則,請(qǐng)使用工廠模式模擬上述 過程。簡單工廠模式public class XMLUtilReas on/該方法用于從 XML配置文件中提取品牌名稱,并返回該品牌名稱public static String getexcuse()try/創(chuàng)建文檔對(duì)象Docume ntBuilderFactory dFactory = Docume ntBuilderFactory .newln sta nce();Docume ntBuilder builder = dFactory .n ewDocume n

15、tBuilder();Docume nt doc;doc = builder.parse( new File("c on figReas on. xml");/獲取包含品牌名稱的文本節(jié)點(diǎn)NodeList nl = doc.getEleme ntsByTagName("excuse");Node classNode=n l.item(O).getFirstChild();String excuse=classNode.getNodeValue().trim();retur n excuse;catch(Excepti on e)e.pri ntStackT

16、race();return nu II;public abstract class AbstractReas on public abstract void reas on();public class GoHome exte nds AbstractReas on public void reas on() public class GoToilet exte nds AbstractReas on public void reas on() public class On TheWay exte nds AbstractReas on public void reas on() publi

17、c class ReasonFactory public static AbstractReason produceReason (String excuse) throws Exceptionif(excuse.equalsIgnoreCase("Toilet")else if(excuse.equalsIgnoreCase("Home")else if(excuse.equalsIgnoreCase("OnTheWay")else throw new Exception("Sorry! The reason is not

18、 true!");public class Client_xml public static void main(String args) tryAbstractReason abstractreason;String excuse=XMLUtilReason.getexcuse();abstractreason=ReasonFduceReason(excuse);/abstractreason=ReasonFduceReason ("Home"); abstractreason.reason();catch(Excepti

19、on e)public class Client public static void main(String args) tryAbstractReason abstractreason;abstractreas on=Reas onF duceReas on ("Toilet"); abstractreas on. reas on();catch(Excepti on e)3. 簡歷題目(淺克?。┬枰幸粋€(gè)簡歷類,必須要有姓名,可以設(shè)置性別和年齡,可以設(shè)置工作經(jīng)歷。最終需要三 份簡歷。Object+clone() : ObjectResume-nam

20、e : String-sex : StringWorkExperience-workDate : String -Company : String*1+setworkDate(in workDate : String) : void +setCompany(in Company : String) : void +getworkDate() : void+getCompany() : void匸-workexperience-age : String-workexperience : WorkExperience = null+Resume(in name : String)+clone()

21、: Object+setName(in name : String) : void+setPersonallnfo(in sex : String, in age : String) : void +getName() : String+getSex() : String+getAge() : String+setWorExperience(in workDate : String, in Company : S +getWorkExperience() : WorkExperience+display() : voidpublic class Resume impleme nts Cion

22、eable 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) =name;public void setPersonalInfo(String sex, String age) this.sex=

23、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); workexperience.setCompany(Company);public WorkExperience ge

24、tWorkExperience()return this.workexperience;public Object clone()Resume clone=null;try clone=(Resume)super.clone(); catch(CloneNotSupportedException e) return clone;public void display()public class WorkExperience private String workDate;private String Company;public void setworkDate(String workDate

25、)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)Resume resume,copy1,copy2;resume=new Resume("zhangsan"

26、);resume.setPersonalInfo("male", "25"); resume.setWorkExperience("1995-1997","Company1"); copy1=(Resume)resume.clone();copy2=(Resume)resume.clone();resume.display();copy1.setName("lisi"); copy1.setWorkExperience("2004-2007","Company2&q

27、uot;);copy1.display();copy2.setPersonalInfo("female", "30");copy2.display();陳俊生版public class Resume implements Cloneable private String name;/ 姓名 private String sex;/ 性別 private int age;/ 年齡private WorkExperience we ;/ 工作經(jīng)歷public Resume()this.we = new WorkExperience();/* 姓名* retu

28、rn*/public String getName() 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;/* 獲取工作經(jīng)歷對(duì)象*/public WorkEx

29、perience getWorkExperience() 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, "設(shè)置成功 n"+str); public static void main(String args) / TODO Auto-generated method stubResume resume , cr1 ,cr2 ,cr3; WorkExperience we1, we2, we3; resume = new Resume()

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論