中南大學(xué)軟件體系結(jié)構(gòu)實(shí)驗(yàn)行為型設(shè)計(jì)模式實(shí)驗(yàn)_第1頁(yè)
中南大學(xué)軟件體系結(jié)構(gòu)實(shí)驗(yàn)行為型設(shè)計(jì)模式實(shí)驗(yàn)_第2頁(yè)
中南大學(xué)軟件體系結(jié)構(gòu)實(shí)驗(yàn)行為型設(shè)計(jì)模式實(shí)驗(yàn)_第3頁(yè)
中南大學(xué)軟件體系結(jié)構(gòu)實(shí)驗(yàn)行為型設(shè)計(jì)模式實(shí)驗(yàn)_第4頁(yè)
中南大學(xué)軟件體系結(jié)構(gòu)實(shí)驗(yàn)行為型設(shè)計(jì)模式實(shí)驗(yàn)_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、軟件體系結(jié)構(gòu)實(shí)驗(yàn)報(bào)告項(xiàng)目名稱 行為型設(shè)計(jì)模式實(shí)驗(yàn) 專業(yè)班級(jí) 學(xué) 號(hào) 姓 名 實(shí)驗(yàn)成績(jī):批閱教師:年 月 日實(shí)驗(yàn)6 行為型設(shè)計(jì)模式實(shí)驗(yàn)實(shí)驗(yàn)學(xué)時(shí): 2 每組人數(shù): 1 實(shí)驗(yàn)類型: 3 (1:基礎(chǔ)性 2:綜合性 3:設(shè)計(jì)性 4:研究性)實(shí)驗(yàn)要求: 1 (1:必修 2:選修 3:其它)實(shí)驗(yàn)類別: 3 (1:基礎(chǔ) 2:專業(yè)基礎(chǔ) 3:專業(yè) 4:其它)一、實(shí)驗(yàn)?zāi)康氖炀毷褂肞owerDesigner和任意一種面向?qū)ο缶幊陶Z(yǔ)言實(shí)現(xiàn)幾種常見(jiàn)的行為型設(shè)計(jì)模式,包括觀察者模式和策略模式,理解每一種設(shè)計(jì)模式的模式動(dòng)機(jī),掌握模式結(jié)構(gòu),學(xué)習(xí)如何使用代碼實(shí)現(xiàn)這些模式。二、實(shí)驗(yàn)內(nèi)容1. 某實(shí)時(shí)在線股票軟件需要提供如下功能:當(dāng)股

2、票購(gòu)買者所購(gòu)買的某支股票價(jià)格變化幅度達(dá)到5%時(shí),系統(tǒng)將自動(dòng)發(fā)送通知(包括新價(jià)格)給購(gòu)買該股票的所有股民。試使用觀察者模式設(shè)計(jì)并實(shí)現(xiàn)該系統(tǒng),要求繪制相應(yīng)的類圖并編程模擬實(shí)現(xiàn)。2. 某公司欲開(kāi)發(fā)一套機(jī)房監(jiān)控系統(tǒng),如果機(jī)房達(dá)到某一指定溫度,溫度傳感器(Thermosensor)將自動(dòng)傳遞信號(hào)給各種響應(yīng)設(shè)備,例如警示燈(CautionLight)將閃爍(flicker()、報(bào)警器(Annunciator)將發(fā)出警報(bào)(alarm()、安全逃生門(SecurityDoor)將自動(dòng)開(kāi)啟(open()、隔熱門(InsulatedDoor)將自動(dòng)關(guān)閉(close()等,每一種響應(yīng)設(shè)備的行為由專門的程序來(lái)控制。為

3、了將來(lái)能夠方便地引入新類型的響應(yīng)設(shè)備,采用觀察者模式和適配器模式設(shè)計(jì)并模擬實(shí)現(xiàn)該系統(tǒng)。3. 某軟件公司為某電影院開(kāi)發(fā)了一套影院售票系統(tǒng),在該系統(tǒng)中需要為不同類型的用戶提供不同的電影票(MovieTicket)打折(Discount)方式,具體打折方案如下:(1) 學(xué)生憑學(xué)生證可享受票價(jià)8折優(yōu)惠;(2) 年齡在10周歲及以下的兒童可享受每張票減免10元的優(yōu)惠(原始票價(jià)需大于等于20元);(3) 影院VIP用戶除享受票價(jià)半價(jià)優(yōu)惠外還可進(jìn)行積分,積分累計(jì)到一定額度可換取電影院贈(zèng)送的獎(jiǎng)品。該系統(tǒng)在將來(lái)可能還要根據(jù)需要引入新的打折方式。試使用策略模式設(shè)計(jì)并編程模擬實(shí)現(xiàn)該影院售票系統(tǒng)。4. 某軟件公司欲開(kāi)

4、發(fā)一款飛機(jī)模擬系統(tǒng),該系統(tǒng)主要模擬不同種類飛機(jī)的飛行特征與起飛特征,需要模擬的飛機(jī)種類及其特征如表1所示:表1 飛機(jī)種類及特征一覽表飛機(jī)種類起飛特征飛行特征直升機(jī)(Helicopter)垂直起飛(VerticalTakeOff)亞音速飛行(SubSonicFly)客機(jī)(AirPlane)長(zhǎng)距離起飛(LongDistanceTakeOff)亞音速飛行(SubSonicFly)殲擊機(jī)(Fighter)長(zhǎng)距離起飛(LongDistanceTakeOff)超音速飛行(SuperSonicFly)鷂式戰(zhàn)斗機(jī)(Harrier)垂直起飛(VerticalTakeOff)超音速飛行(SuperSonicFly

5、)為將來(lái)能夠模擬更多種類的飛機(jī),試采用策略模式設(shè)計(jì)并模擬實(shí)現(xiàn)該飛機(jī)模擬系統(tǒng)。三、實(shí)驗(yàn)要求1. 結(jié)合實(shí)例,正確無(wú)誤地繪制觀察者模式和策略模式的模式結(jié)構(gòu)圖;2. 使用任意一種面向?qū)ο缶幊陶Z(yǔ)言實(shí)現(xiàn)觀察者模式和策略模式實(shí)例,代碼運(yùn)行正確無(wú)誤。四、實(shí)驗(yàn)步驟1. 結(jié)合實(shí)例,使用PowerDesigner繪制觀察者模式實(shí)例結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z(yǔ)言實(shí)現(xiàn)該模式實(shí)例;2. 結(jié)合實(shí)例,使用PowerDesigner繪制觀察者模式實(shí)例結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z(yǔ)言實(shí)現(xiàn)該模式實(shí)例;3. 結(jié)合實(shí)例,使用PowerDesigner繪制策略模式實(shí)例結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z(yǔ)言實(shí)現(xiàn)該模式實(shí)例;4. 結(jié)合實(shí)例,使用PowerDesi

6、gner繪制策略模式實(shí)例結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z(yǔ)言實(shí)現(xiàn)該模式實(shí)例。五、實(shí)驗(yàn)結(jié)果1. 類圖:實(shí)現(xiàn)代碼:public class Client public static void main(String args)SharesSubject share1 = new ConcreteShares(100);Invester observer1 = new Invester("張三");Invester observer2 = new Invester("李四");share1.addInvester(observer1);share1.addInveste

7、r(observer2);share1.change();/* * Module: ConcreteShares.java * Author: ct * Purpose: Defines the Class ConcreteShares */import java.util.*;/* pdOid 03891fd5-7c63-4b84-9bbf-2430332d48d2 */public class ConcreteShares extends SharesSubject /* pdOid ab879b87-ee8a-4265-93ea-a9f2ef3b54d9 */private double

8、 price;ConcreteShares(double newPrice)price = newPrice;public void setPrice(double newPrice)price = newPrice;public double getPrice()return price; public void change() / TODO: implement System.out.println("變化超過(guò)5%"); System.out.println("-"); for(MyObserver obs:observer) (MyObserve

9、r)obs).getChange(price); /* * Module: Invester.java * Author: ct * Purpose: Defines the Class Invester */import java.util.*;/* pdOid a295ff58-e079-42a6-a1d9-f741697dae60 */public class Invester implements MyObserver public String name;Invester(String name) = name; public void getChange(doub

10、le Price) / TODO: implement System.out.println("正在發(fā)送信息給用戶"+name+",目前該股票價(jià)格為"+Price); /* * Module: MyObserver.java * Author: ct * Purpose: Defines the Interface MyObserver */import java.util.*;/* pdOid 995b8e6a-43c4-4fa9-99ca-10b78081beae */public interface MyObserver /* pdOid 463b

11、82d3-31e1-4c0a-afae-74595234ab82 */ public void getChange(double Price);/* * Module: SharesSubject.java * Author: ct * Purpose: Defines the Class SharesSubject */import java.util.*;/* pdOid 23f9decd-a8e4-4e34-9cf2-6abf8fcc3678 */public abstract class SharesSubject /* pdOid 06f9c8c6-c557-4a15-8bb9-bc

12、2034891d68 */ public ArrayList<MyObserver> observer = new ArrayList(); public void addInvester(MyObserver newObserver) / TODO: implement observer.add(newObserver); /* pdOid 3050e00a-10fc-4cb8-9492-222fcc9964a2 */ public void removeInvester(MyObserver oldObserver) / TODO: implement observer.rem

13、ove(oldObserver); /* pdOid 7dc77e25-a4ab-4193-96bc-bca75699feaf */ public abstract void change(); 2. 類圖:實(shí)現(xiàn)代碼:v/* * Module: Annunciatior.java * Author: ct * Purpose: Defines the Class Annunciatior */import java.util.*;/* pdOid 3510b9e2-117f-483b-8974-09e848391cf1 */public class Annunciator /* pdOid a

14、6c25fe7-e2d8-42da-ba61-b75314a19725 */ public void alarm() / TODO: implement System.out.println("警報(bào)響起"); /* * Module: AnnuciatorObserver.java * Author: ct * Purpose: Defines the Class AnnuciatorObserver */import java.util.*;/* pdOid 30002f48-45d3-4162-94aa-a9d00c4768ec */public class Annun

15、ciatorObserver implements MyObserver /* pdOid 7b599933-3f2b-46e5-8f57-96eb28d90d5c */ private Annunciator annunciator; AnnunciatorObserver(Annunciator newAnnunciator) this.annunciator = newAnnunciator; /* pdOid cc9d6b91-6264-4d80-804d-cb8a933fc46c */ public void response() / TODO: implement annuncia

16、tor.alarm(); /* * Module: CautionLight.java * Author: ct * Purpose: Defines the Class CautionLight */import java.util.*;/* pdOid 5cf7cee6-d049-4109-a349-801aa6589072 */public class CautionLight /* pdOid 212c62b0-7f33-44ba-a43e-7e9754286e6a */ public void flicker() / TODO: implement System.out.printl

17、n("應(yīng)急燈打開(kāi)"); /* * Module: CautionLightObserver.java * Author: ct * Purpose: Defines the Class CautionLightObserver */import java.util.*;/* pdOid 438ef70a-32ca-4bb3-a07e-71d6606642e5 */public class CautionLightObserver implements MyObserver /* pdOid 0887aded-cca4-490d-af3c-203073d8356b */ pr

18、ivate CautionLight cautionLight; CautionLightObserver(CautionLight newCautionLight) this.cautionLight = newCautionLight; /* pdOid 6cba0a71-1bc7-4052-8f95-b9ad83e34d2a */ public void response() / TODO: implement cautionLight.flicker(); public class Client public static void main(String args)MySubject

19、 tempreture = new Tempreture();MyObserver annunciatorObserver,cautionLightObserver,insulatedDoorObserver,securityDoorObserver;Annunciator annunciator = new Annunciator();CautionLight cautionLight = new CautionLight();InsulatedDoor insulatedDoor = new InsulatedDoor();SecurityDoor securityDoor = new S

20、ecurityDoor();annunciatorObserver = new AnnunciatorObserver(annunciator);cautionLightObserver = new CautionLightObserver(cautionLight);insulatedDoorObserver = new InsulatedDoorObserver(insulatedDoor);securityDoorObserver = new SecurityDoorObserver(securityDoor);tempreture.add(securityDoorObserver);t

21、empreture.add(insulatedDoorObserver);tempreture.add(cautionLightObserver);tempreture.add(annunciatorObserver);tempreture.change();/* * Module: InsulatedDoor.java * Author: ct * Purpose: Defines the Class InsulatedDoor */import java.util.*;/* pdOid f495d797-be3a-4141-9d50-325742eb46ab */public class

22、InsulatedDoor /* pdOid 7540dd3b-1f25-4d54-8828-73222f87e612 */ public void close() / TODO: implement System.out.println("隔離門關(guān)閉"); /* * Module: InsulatedDoorObserver.java * Author: ct * Purpose: Defines the Class InsulatedDoorObserver */import java.util.*;/* pdOid 26920542-c6ee-4b65-b8a4-a5

23、5c13119475 */public class InsulatedDoorObserver implements MyObserver /* pdOid 9da21a0d-9858-485c-9d9b-5e3fc4e7c652 */ private InsulatedDoor insulatedDoor; InsulatedDoorObserver(InsulatedDoor newInsulatedDoor) this.insulatedDoor = newInsulatedDoor; /* pdOid 2044ceef-9d41-474b-be4d-8feb1aeb828a */ pu

24、blic void response() / TODO: implement insulatedDoor.close(); /* * Module: MyObserver.java * Author: ct * Purpose: Defines the Interface MyObserver */import java.util.*;/* pdOid 2414fcb6-7a72-4e74-b7e4-ff02a04c2264 */public interface MyObserver /* pdOid be86483c-16d5-4f53-a1f1-a12f56fb13c6 */ void r

25、esponse();/* * Module: MySubject.java * Author: ct * Purpose: Defines the Class MySubject */import java.util.*;/* pdOid 0af97a55-e330-4640-abe4-26dc1dc49144 */public abstract class MySubject /* pdOid 60bd7861-80b9-4756-893b-e294a09a9262 */ protected ArrayList<MyObserver> observer= new ArrayLis

26、t(); /* param newobserver * pdOid 8f093415-73f6-40fd-b6ef-c82034c60bc7 */ public void add(MyObserver newobserver) / TODO: implement observer.add(newobserver); /* param old * pdOid e1b7ad74-946a-48f7-b813-c55737f4dd96 */ public void remove(MyObserver old) / TODO: implement observer.remove(old); /* pd

27、Oid 82c76231-cb91-499b-8749-e5e9fc3fc365 */ public abstract void change(); /* * Module: SecurityDoor.java * Author: ct * Purpose: Defines the Class SecurityDoor */import java.util.*;/* pdOid bc24bd59-23fe-44fd-b1ae-bbbe132f467a */public class SecurityDoor /* pdOid 2925ef22-eec2-4758-bf42-1c32d181406

28、0 */ public void open() / TODO: implement System.out.println("安全門打開(kāi)"); /* * Module: SecurityDoorObserver.java * Author: ct * Purpose: Defines the Class SecurityDoorObserver */import java.util.*;/* pdOid 754901d0-b712-40a7-8097-718c2e4000ec */public class SecurityDoorObserver implements MyO

29、bserver /* pdOid 7f9d61ef-36f4-487d-b290-0ddf20df401f */ private SecurityDoor securityDoor; SecurityDoorObserver(SecurityDoor newSecurityDoor) this.securityDoor = newSecurityDoor; /* pdOid 78b7cc18-a6b4-416c-aecb-a87580c4c50d */ public void response() / TODO: implement securityDoor.open(); 3. 類圖:實(shí)現(xiàn)代

30、碼:/* * Module: ChildrenDiscount.java * Author: ct * Purpose: Defines the Class ChildrenDiscount */import java.util.*;/* pdOid 40bf41f9-236d-421e-a4cb-c28e520efd0d */public class ChildrenDiscount implements Discount /* pdOid 9832b602-b5e6-4fdf-9eef-b823cfcaafc0 */ public void discount() / TODO: imple

31、ment System.out.println("年齡在10周歲及以下的兒童可享受每張票減免10元的優(yōu)惠(原始票價(jià)需大于等于20元)"); public class Client public static void main(String args)MovieTicket movieTicket = new MovieTicket();Discount studentDiscount,vipDiscount,childrenDiscount;studentDiscount = new StudentDiscount();vipDiscount = new VIPDisco

32、unt();childrenDiscount = new ChildrenDiscount();movieTicket.setDiscount(studentDiscount);movieTicket.discount();movieTicket.setDiscount(childrenDiscount);movieTicket.discount();/* * Module: Discount.java * Author: ct * Purpose: Defines the Interface Discount */import java.util.*;/* pdOid ab09e5e6-a6

33、12-4b8b-8713-b313bee1f1b2 */public interface Discount /* pdOid 7cdc2442-371c-4b9b-be7f-d3878eb29b89 */ void discount();/* * Module: MovieTicket.java * Author: ct * Purpose: Defines the Class MovieTicket */import java.util.*;/* pdOid 28048305-6bd5-4e91-945a-890457a2b17b */public class MovieTicket /*

34、pdOid 041754c6-86bd-4a6e-910a-887ec3775bda */ private Discount discount; /* param discoutn * pdOid b2db074a-345d-4eae-b390-fea81a41ac1d */ public void setDiscount(Discount discount) / TODO: implement this.discount = discount; /* pdOid ef01351f-b74d-4765-af6d-e11813b3b53c */ public void discount() /

35、TODO: implement discount.discount(); /* * Module: StudentDiscount.java * Author: ct * Purpose: Defines the Class StudentDiscount */import java.util.*;/* pdOid 17d717dd-fcc8-4205-9965-503b708ac510 */public class StudentDiscount implements Discount /* pdOid ff1afcce-6145-4516-8a06-e6759f809323 */ publ

36、ic void discount() / TODO: implement System.out.println("學(xué)生憑學(xué)生證可享受票價(jià)8折優(yōu)惠;"); /* * Module: VIPDiscount.java * Author: ct * Purpose: Defines the Class VIPDiscount */import java.util.*;/* pdOid 716e64ca-a980-4447-a5a2-78ff105cb655 */public class VIPDiscount implements Discount /* pdOid 06f015

37、57-deff-4917-b797-7b3625ba65a7 */ public void discount() / TODO: implement System.out.println("影院VIP用戶除享受票價(jià)半價(jià)優(yōu)惠外還可進(jìn)行積分,積分累計(jì)到一定額度可換取電影院贈(zèng)送的獎(jiǎng)品。"); 4. 類圖:實(shí)現(xiàn)代碼:/* * Module: Air.java * Author: ct * Purpose: Defines the Class Air */import java.util.*;/* pdOid e1360518-69e6-4e5a-9e93-3bf21d872ba0

38、*/public abstract class Air /* pdOid 2c2a7f6d-d49c-4597-b6c6-c31dffa5c80e */ protected TakeOff takeOff; /* pdOid 07984440-50a4-4c60-8761-92343ac941e4 */ protected Fly fly; public void setTakeOff(TakeOff newTakeOff) this.takeOff = newTakeOff; public void setFly(Fly newFly) this.fly = newFly; /* pdOid

39、 c3df320d-0c7b-4d6b-841c-04f3125a0ed1 */ public abstract void takeOff(); public abstract void fly() ;/* * Module: Airplane.java * Author: ct * Purpose: Defines the Class Airplane */import java.util.*;/* pdOid 510ff8af-57dc-4dcf-bd9f-6f04d926ffec */public class Airplane extends Air public void takeOf

40、f()System.out.print("客機(jī)正在模擬");takeOff.takeOff();public void fly()System.out.print("客機(jī)正在模擬");fly.fly();public class Client public static void main (String args)Air air;air = new Helicopter();Fly fly = new SubSonicFly();air.setFly(fly);TakeOff takeOff = new VerticalTakeOff();air.se

41、tTakeOff(takeOff);air.takeOff();air.fly();/* * Module: Flighter.java * Author: ct * Purpose: Defines the Class Flighter */import java.util.*;/* pdOid 64290630-333c-4a63-ac6c-3325225c9dbf */public class Flighter extends Air public void takeOff()System.out.print("戰(zhàn)斗機(jī)正在模擬");takeOff.takeOff();public void fly()System.out.print("戰(zhàn)斗機(jī)正在模擬");fly.fly();/* * Module: Fly.j

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論