組合,外觀,迭代器模式案例實(shí)驗(yàn)報(bào)告_第1頁(yè)
組合,外觀,迭代器模式案例實(shí)驗(yàn)報(bào)告_第2頁(yè)
組合,外觀,迭代器模式案例實(shí)驗(yàn)報(bào)告_第3頁(yè)
組合,外觀,迭代器模式案例實(shí)驗(yàn)報(bào)告_第4頁(yè)
組合,外觀,迭代器模式案例實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩9頁(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、 軟件設(shè)計(jì)與體系結(jié)構(gòu)實(shí) 驗(yàn) 報(bào) 告課程名稱軟件設(shè)計(jì)與體系結(jié)構(gòu)課程編號(hào)0920116實(shí)驗(yàn)項(xiàng)目名稱 組合,外觀,迭代器模式案例學(xué)號(hào)班級(jí)姓名專業(yè)學(xué)生所在學(xué)院指導(dǎo)教師實(shí)驗(yàn)室名稱地點(diǎn)實(shí)驗(yàn)時(shí)間 實(shí)驗(yàn)名稱: 迭代器,外觀,組合模式案例 實(shí)驗(yàn)?zāi)康模旱?,外觀,組合模式(Observer Pattern)是設(shè)計(jì)模式中行為模式的一種,它解決了上述具有一對(duì)多依賴關(guān)系的對(duì)象的重用問(wèn)題。此模式的參與者分為兩大類,一類是被觀察的目標(biāo),另一類是觀察該目標(biāo)的觀察者們。正因?yàn)樵撃J绞腔凇耙粚?duì)多”的關(guān)系,所以該模式一般是應(yīng)用于由一個(gè)目標(biāo)對(duì)象和N個(gè)觀察者對(duì)象組成(當(dāng)然也可以擴(kuò)展為有多個(gè)目標(biāo)對(duì)象,但我們現(xiàn)在只討論前者)的場(chǎng)合。當(dāng)

2、目標(biāo)對(duì)象的狀態(tài)發(fā)生改變或做出某種行為時(shí),正在觀察該目標(biāo)對(duì)象的觀察者們將自動(dòng)地、連鎖地作出相應(yīng)的響應(yīng)行為。 通過(guò)本次實(shí)驗(yàn)了解觀察者模式的原理。并能夠運(yùn)用觀察者模式來(lái)進(jìn)行編程。實(shí)驗(yàn)內(nèi)容1 UML類圖迭代器模式:組合模式:外觀模式?jīng)_茶2 程序的源代碼 迭代器: package Iterator;import java.util.Enumeration;public class MyEnumeration private Enumeration me;private int step;public Enumeration getMe() return me;public void setMe(Enum

3、eration me) this.me = me;public int getStep() return step;public void setStep(int step) this.step = step;public MyEnumeration(Enumeration e) me = e;public void traverse() while (me.hasMoreElements() System.out.println(me.nextElement();public void jumptraverse() while (me.hasMoreElements() System.out

4、.println(me.nextElement();for (int i = 1; i < step; i+) if (me.hasMoreElements()me.nextElement();package Iterator;import java.util.Enumeration;import java.util.Vector;public class Test public static void main(String args) / TODO Auto-generated method stubVector v =new Vector<>();v.add(3);v.

5、add(6);v.add(9);v.add(12);v.add(15);v.add(18);v.add(21);v.add(24);v.add(27);Enumeration<Vector> e=v.elements();MyEnumeration m=new MyEnumeration(e);System.out.println("normal traverse:");/m.traverse();m.setStep(3);System.out.println("jump traverse:");m.jumptraverse();組合模式:p

6、ackage Combination;public abstract class Company protected String name;public Company(String s)name=s;public abstract void add(Company c);public abstract void remove(Company c);public abstract void display(int depth);public abstract void lineOfDuty();package Combination;public class FinanceDepartmen

7、t extends Company public FinanceDepartment(String s) super(s);/ TODO Auto-generated constructor stubOverridepublic void add(Company c) / TODO Auto-generated method stubOverridepublic void remove(Company c) / TODO Auto-generated method stubOverridepublic void display(int depth) / TODO Auto-generated

8、method stubString s=""for(int i=0;i<depth;i+)s=s+'-'System.out.println(s+name);Overridepublic void lineOfDuty() / TODO Auto-generated method stubSystem.out.println(name+" 負(fù)責(zé)管理公司財(cái)務(wù)收支");package Combination;import java.io.Serializable;import java.util.ArrayList;import jav

9、a.util.Collection;import java.util.Iterator;import java.util.List;import java.util.ListIterator;public class ConcreteCompany extends Company implements Serializableprivate List<Company> children;public ConcreteCompany(String s) super(s);children=new ArrayList<Company>();public void add(C

10、ompany c) / TOO Auto-generated method stubtrychildren.add(c);catch(Exception e)if(children=null)System.out.println("cccc=null");public void remove(Company c) / TODO Auto-generated method stubchildren.remove(c);public void display(int depth) / TODO Auto-generated method stubString s="&

11、quot;for(int i=0;i<depth;i+)s=s+'-'System.out.println(s+name);ListIterator<Company> i = children.listIterator();while(i.hasNext()i.next().display(depth+2);public void lineOfDuty() / TODO Auto-generated method stubIterator<Company> i=children.iterator();while(i.hasNext()i.next(

12、).lineOfDuty();package Combination;public class HRdepartment extends Company public HRdepartment(String s) super(s);/ TODO Auto-generated constructor stubOverridepublic void add(Company c) / TODO Auto-generated method stubOverridepublic void remove(Company c) / TODO Auto-generated method stubOverrid

13、epublic void display(int depth) / TODO Auto-generated method stubString s=""for(int i=0;i<depth;i+)s=s+'-'System.out.println(s+name);Overridepublic void lineOfDuty() / TODO Auto-generated method stubSystem.out.println(name+" 負(fù)責(zé)員工招聘培訓(xùn)管理");package Combination;public clas

14、s Test public static void main(String args) ConcreteCompany root=new ConcreteCompany("北京總公司");root.add(new HRdepartment("總公司人力資源部");root.add(new FinanceDepartment("總公司財(cái)務(wù)部");ConcreteCompany comp=new ConcreteCompany("上海華東分公司");comp.add(new HRdepartment("華東分

15、公司人力資源部");comp.add(new FinanceDepartment("華東分公司財(cái)務(wù)部");root.add(comp);ConcreteCompany comp1=new ConcreteCompany("南京辦事處");comp1.add(new HRdepartment("南京辦事處人力資源部");comp1.add(new FinanceDepartment("南京辦事處財(cái)務(wù)部");comp.add(comp1);ConcreteCompany comp2=new ConcreteC

16、ompany("杭州辦事處");comp2.add(new HRdepartment("杭州辦事處人力資源部");comp2.add(new FinanceDepartment("杭州辦事處財(cái)務(wù)部");comp.add(comp2);System.out.println("公司結(jié)構(gòu)圖");root.display(1);System.out.println("各部門職責(zé)");root.lineOfDuty();外觀模式?jīng)_茶:public class TeaBag public TeaBag()

17、System.out.println("清香的茶包準(zhǔn)備好了"); public class Water boolean waterIsBoiling; public Water () setWaterIsBoiling (false); System.out.println ("純凈的水準(zhǔn)備好了"); public void boilFacadeWater () setWaterIsBoiling (true); System.out.println ("水在沸騰"); public void setWaterIsBoiling (b

18、oolean isWaterBoiling) waterIsBoiling = isWaterBoiling; public boolean getWaterIsBoiling () return waterIsBoiling; public class TeaCup boolean teaBagIsSteeped; Water facadeWater; TeaBag facadeTeaBag; public TeaCup () setTeaBagIsSteeped (false); System.out.println ("茶杯準(zhǔn)備好了"); public void se

19、tTeaBagIsSteeped (boolean isTeaBagSteeped) teaBagIsSteeped = isTeaBagSteeped; public boolean getTeaBagIsSteeped () return teaBagIsSteeped; public void addFacadeTeaBag (TeaBag facadeTeaBagIn) facadeTeaBag = facadeTeaBagIn; System.out.println ("茶包放在苶杯了"); public void addFacadeWater (Water fa

20、cadeWaterIn) facadeWater = facadeWaterIn; System.out.println ("水倒入杯子了"); public void steepTeaBag () if ( (facadeTeaBag != null) && ( (facadeWater != null) && (facadeWater.getWaterIsBoiling () System.out.println ("茶滲透到杯子中"); setTeaBagIsSteeped (true); else System.o

21、ut.println ("茶沒(méi)有滲透到杯子中"); setTeaBagIsSteeped (false); public String toString() if (this.getTeaBagIsSteeped() return ("一杯又香又濃的茶沖好了!"); else String tempString = new String ("一個(gè)杯子"); if (facadeWater != null) if (facadeWater.getWaterIsBoiling () tempString = (tempString + &

22、quot;放有沸騰的水 "); else tempString = (tempString + "放有凍水 "); else tempString = (tempString + "沒(méi)有放水 "); if (facadeTeaBag != null) tempString = (tempString + "有一個(gè)茶包"); else tempString = (tempString + "沒(méi)有茶包"); return tempString; public class FacadeCuppaMaker bo

23、olean teaBagIsSteeped; public FacadeCuppaMaker() System.out.println("FacadeCuppaMaker 準(zhǔn)備可以為你沖茶了!"); public TeaCup makeACuppa() TeaCup cup = new TeaCup(); TeaBag teaBag = new TeaBag(); Water water = new Water(); cup.addFacadeTeaBag(teaBag); water.boilFacadeWater(); cup.addFacadeWater(water); cup.steepTeaBag(); return cup; public class Client public static void main (String args) FacadeCuppaMaker cuppaMaker = new FacadeCuppaMa

溫馨提示

  • 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)論