版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
設(shè)計(jì)模式試驗(yàn)一實(shí)驗(yàn)報(bào)告書專業(yè)班級(jí)軟件0703學(xué)號(hào)姓名吉亞云指導(dǎo)老師劉偉時(shí)間3月27日中南大學(xué)軟件學(xué)院試驗(yàn)一設(shè)計(jì)模式上機(jī)試驗(yàn)一一、試驗(yàn)?zāi)繕?biāo)熟練使用PowerDesigner繪制類圖,結(jié)構(gòu)系統(tǒng)靜態(tài)結(jié)構(gòu)模型;使用PowerDesigner和任意一個(gè)面向?qū)ο缶幊陶Z言實(shí)現(xiàn)幾個(gè)常見設(shè)計(jì)模式,包含簡(jiǎn)單工廠模式、工廠方法模式、抽象工廠模式、原型模式、單例模式、適配器模式、橋接模式和組合模式。二、試驗(yàn)要求1.使用PowerDesigner繪制標(biāo)準(zhǔn)UML類圖;2.正確無誤繪制簡(jiǎn)單工廠模式、工廠方法模式、抽象工廠模式、原型模式、單例模式、適配器模式、橋接模式和組合模式模式結(jié)構(gòu)圖;3.使用任意一個(gè)面向?qū)ο缶幊陶Z言實(shí)現(xiàn)簡(jiǎn)單工廠模式、工廠方法模式、抽象工廠模式、原型模式、單例模式、適配器模式、橋接模式和組合模式,代碼運(yùn)行正確無誤。三、試驗(yàn)步驟1.熟悉PowerDesigner11慣用功效;2.使用PowerDesigner繪制類圖;3.使用PowerDesigner繪制簡(jiǎn)單工廠模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;4.使用PowerDesigner繪制工廠方法模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;5.使用PowerDesigner繪制抽象工廠模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;6.使用PowerDesigner繪制原型模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;7.使用PowerDesigner繪制單例模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;8.使用PowerDesigner繪制適配器模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;9.使用PowerDesigner繪制橋接模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式;10.使用PowerDesigner繪制組合模式結(jié)構(gòu)圖并用面向?qū)ο缶幊陶Z言實(shí)現(xiàn)該模式。四、試驗(yàn)匯報(bào)要求1.繪制實(shí)例場(chǎng)景類圖;2.提供簡(jiǎn)單工廠模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;3.提供工廠方法模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;4.提供抽象工廠模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;5.提供原型模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;6.提供單例模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;7.提供適配器模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;8.提供橋接模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼;9.提供組合模式結(jié)構(gòu)圖及實(shí)當(dāng)代碼。五、試驗(yàn)結(jié)果使用PowerDesigner繪制類圖;用PowerDesigner和任意一個(gè)面向?qū)ο缶幊陶Z言實(shí)現(xiàn)簡(jiǎn)單工廠模式、工廠方法模式、抽象工廠模式、原型模式、單例模式、適配器模式、橋接模式和組合模式,包含依照實(shí)例繪制模式結(jié)構(gòu)圖、編寫模式實(shí)當(dāng)代碼,運(yùn)行并測(cè)試模式實(shí)例代碼。(1)類圖某音像圖書出租企業(yè)欲開發(fā)一個(gè)音像圖書租賃信息系統(tǒng),處理日常音像圖書租借業(yè)務(wù)。該系統(tǒng)將統(tǒng)計(jì)全部圖書信息、唱碟信息、用戶信息和用戶租借信息。現(xiàn)決定采取面向?qū)ο蠓治龊驮O(shè)計(jì)方法來開發(fā)此系統(tǒng),經(jīng)過初步分析,設(shè)計(jì)人員定義了以下初始實(shí)體類:●Book(圖書)類屬性:no(編號(hào))、title(標(biāo)題)、author(作者)、isbn(書號(hào))、press(出版社)、price(價(jià)格)。方法:每個(gè)屬性get()方法和set()方法?!馜isc(唱碟)類屬性:no(編號(hào))、title(標(biāo)題)、singer(歌手)、producer(制作企業(yè))、price(價(jià)格)。方法:每個(gè)屬性get()方法和set()方法。●Customer(客戶)類屬性:cno(客戶編號(hào))、name(姓名)、address(地址)、tel(電話)。方法:每個(gè)屬性get()方法和set()方法?!馤oanItem(可租項(xiàng)目)類統(tǒng)計(jì)Book和Disc共有屬性和方法?!馬entHistory(租借歷史統(tǒng)計(jì))類屬性:rno(歷史統(tǒng)計(jì)編號(hào))、hireTime(出租時(shí)間)、returnTime(償還時(shí)間)、cno(客戶編號(hào))、no(租借圖書或唱碟編號(hào))、deposit(押金金額)。方法:每個(gè)屬性get()方法和set()方法。在深入分析之后,需要在LoanItem類中定義一個(gè)集合來存放每一個(gè)圖書或唱碟全部租借歷史統(tǒng)計(jì)。依照以上信息,繪制對(duì)應(yīng)實(shí)體類類圖。(2)簡(jiǎn)單工廠模式使用簡(jiǎn)單工廠模式設(shè)計(jì)一個(gè)能夠創(chuàng)建不一樣幾何形狀(Shape)繪圖工具類,如可創(chuàng)建圓形(Circle)、方形(Rectangle)和三角形(Triangle)對(duì)象,每個(gè)幾何圖形都要有繪制draw()和擦除erase()兩個(gè)方法,要求在繪制不支持幾何圖形時(shí),提醒一個(gè)UnsupportedShapeException,繪制類圖并編程實(shí)現(xiàn)。//抽象圖形interfaceShape{ voiddraw(); voiderase();}classCircleimplementsShape{ publicvoiddraw() { System.out.println("Circledrawing!"); } publicvoiderase() { System.out.println("Circleeraseing!"); } }classRectangleimplementsShape{ publicvoiddraw() { System.out.println("Rectangledrawing!"); } publicvoiderase() { System.out.println("Rectangleeraseing!"); } }classTriangleimplementsShape{ publicvoiddraw() { System.out.println("Triangledrawing!"); } publicvoiderase() { System.out.println("Triangleeraseing!"); } }//工廠classShapeFactory{ //工廠方法factorymethod publicstaticShapeproduceShape(StringshapeName)throwsUnsupportedShapeException { if(shapeName.equalsIgnoreCase("circle")) { returnnewCircle(); } elseif(shapeName.equalsIgnoreCase("rectangle")) { returnnewRectangle(); } elseif(shapeName.equalsIgnoreCase("triangle")) { returnnewTriangle(); } else { thrownewUnsupportedShapeException(); } }}classUnsupportedShapeExceptionextendsException{ publicStringtoString(){ return"繪制不支持該幾何圖形!"; } }classShapeClient{ publicstaticvoidmain(Stringa[]) { Shapecircle=null; try{ circle=ShapeFduceShape("circle"); }catch(UnsupportedShapeExceptione){ e.printStackTrace(); } circle.draw(); circle.erase(); Shapetriangle=null; try{ triangle=ShapeFduceShape("triangle"); }catch(UnsupportedShapeExceptione){ e.printStackTrace(); } triangle.draw(); triangle.erase(); }}(3)工廠方法模式海爾工廠(Haier)生產(chǎn)海爾空調(diào)(HaierAirCondition),美工廠(Midea)生產(chǎn)美空調(diào)(MideaAirCondition)。使用工廠方法模式描述該場(chǎng)景,繪制類圖并編程實(shí)現(xiàn)。interfaceAirCondition{ voidwork();}interfaceAirConditionFactory{ AirConditionproduceAirCondition();}classHaierAirConditionimplementsAirCondition{ publicvoidwork() { System.out.println("HaierAirConditionworking!"); }}classMideaAirConditionimplementsAirCondition{ publicvoidwork() { System.out.println("MideaAirConditionworking!"); }}classHaierAirConditionFactoryimplementsAirConditionFactory{ publicHaierAirConditionproduceAirCondition() { System.out.println("HaierAirConditionproduced!"); returnnew HaierAirCondition(); }}classMideaAirConditionFactoryimplementsAirConditionFactory{ publicMideaAirConditionproduceAirCondition() { System.out.println("MideaAirConditionproduced!"); returnnew MideaAirCondition(); }}classAirConditionClient{ publicstaticvoidmain(Stringa[]) { AirConditionFactorycf; AirConditionairCondition;cf=(AirConditionFactory)XMLUtil.getBean();airCondition=duceAirCondition();airCondition.work(); }}(4)抽象工廠模式電腦配件生產(chǎn)工廠生產(chǎn)內(nèi)存、CPU等硬件設(shè)備,這些內(nèi)存、CPU品牌、型號(hào)并不一定相同,依照下面“產(chǎn)品等級(jí)結(jié)構(gòu)-產(chǎn)品族”示意圖,使用抽象工廠模式實(shí)現(xiàn)電腦配件生產(chǎn)過程并繪制對(duì)應(yīng)類圖,繪制類圖并編程實(shí)現(xiàn)。interfaceCPU{ voiddiscribe();}interfaceRAM{ voiddiscribe();}classPcCPUimplementsCPU{ publicvoiddiscribe() { System.out.println("PcCPU"); }}classMacCPUimplementsCPU{ publicvoiddiscribe() { System.out.println("MacCPU"); }}classPcRAMimplementsRAM{ publicvoiddiscribe() { System.out.println("PcRAM"); }}classMacRAMimplementsRAM{ publicvoiddiscribe() { System.out.println("MacRAM"); }}interfaceComputerPartsFactory{ CPUproduceCPU(); RAMproduceRAM();}classPcFactoryimplementsComputerPartsFactory{ publicPcCPUproduceCPU(){ System.out.println("PcCPUproduced!"); returnnewPcCPU(); } publicPcRAMproduceRAM(){ System.out.println("PcRAMproduced!"); returnnewPcRAM(); }}classMacFactoryimplementsComputerPartsFactory{ publicMacCPUproduceCPU(){ System.out.println("MacCPUproduced!"); returnnewMacCPU(); } publicMacRAMproduceRAM(){ System.out.println("MacRAMproduced!"); returnnewMacRAM(); }}classComputerPartsClient{ publicstaticvoidmain(Stringa[]) { ComputerPartsFactoryfactory; CPUcpu; RAMram; factory=(ComputerPartsFactory)XMLUtil.getBean(); cpu=duceCPU(); cpu.discribe();ram=duceRAM();ram.discribe(); }}(5)原型模式設(shè)計(jì)一個(gè)客戶類Customer,其中客戶地址存放在地址類Address中,用淺克隆和深克隆分別實(shí)現(xiàn)Customer對(duì)象復(fù)制并比較這兩種克隆方式異同。繪制類圖并編程實(shí)現(xiàn)。classAddressimplementsSerializable{publicvoiddisplayAddress(){ System.out.println("address...");}}//淺克隆Customer類classCustomerimplementsCloneable{ privateAddressaddress=null; publicAddressgetAddress(){ returnaddress; } publicvoidsetAddress(Addressaddress){ this.address=address; } publicObjectclone() { Customerclone=null; try { clone=(Customer)super.clone(); }catch(CloneNotSupportedExceptione){ System.out.println("Clonefailure!");} returnclone; } }//深克隆Customer類classCustomerDeepCloneimplementsCloneable,Serializable{ privateAddressaddress=null; publicAddressgetAddress(){ returnaddress; } publicvoidsetAddress(Addressaddress){ this.address=address; } publicObjectclone() { Customerclone=null; try { //使用流來進(jìn)行處理 ByteArrayOutputStreambao=newByteArrayOutputStream(); ObjectOutputStreamoos=newObjectOutputStream(bao); oos.writeObject(this); ByteArrayInputStreambis=newByteArrayInputStream(bao.toByteArray()); ObjectInputStreamois=newObjectInputStream(bis); return(ois.readObject()); }catch(IOExceptione){ e.printStackTrace(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); } returnclone; }}classCustomerClient{ publicstaticvoidmain(Stringa[]) { Customercustomer=newCustomer(); customer.setAddress(newAddress()); CustomercopyCustomer=(Customer)customer.clone(); System.out.println("customer==copyCustomer?"); System.out.println(customer==copyCustomer); System.out.println("customer.getAddress==copyCustomer.getAddress?"); System.out.println(customer.getAddress()==copyCustomer.getAddress()); CustomerDeepClonecustomerDeepClone=newCustomerDeepClone(); customerDeepClone.setAddress(newAddress()); CustomerDeepClonecopyCustomerDeepClone=(CustomerDeepClone)customerDeepClone.clone(); System.out.println("customerDeepClone==copyCustomerDeepClone?"); System.out.println(customerDeepClone==copyCustomerDeepClone); System.out.println("customerDeepClone.getAddress==copyCustomerDeepClone.getAddress?"); System.out.println(customerDeepClone.getAddress()==copyCustomerDeepClone.getAddress()); }}運(yùn)行結(jié)果:(6)單例模式用懶漢式單例實(shí)現(xiàn)在某系統(tǒng)運(yùn)行時(shí),其登錄界面類LoginForm只能夠彈出一個(gè),假如第二次實(shí)例化該類則提醒“程序已運(yùn)行”。繪制類圖并編程實(shí)現(xiàn)。classLoginFormextendsJFrame{ privatestaticLoginFormuniqueInstance=null; privateLoginForm() { //設(shè)置標(biāo)題 this.setTitle("LoginForm"); this.setSize(400,300); //設(shè)置關(guān)閉按鈕事件 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //設(shè)置是否可見 this.setVisible(true); } publicstaticLoginFormgetInstance() { if(uniqueInstance==null) { uniqueInstance=newLoginForm(); }else{ JOptionPane.showMessageDialog(null,"LoginFrame已經(jīng)在運(yùn)行!"); } returnuniqueInstance; }}classLoginFormClient{ publicstaticvoidmain(Stringa[]) { LoginForms1=null,s2=null; s1=LoginForm.getInstance(); s2=LoginForm.getInstance(); System.out.println("s1==s2?"); System.out.println(s1==s2); }}運(yùn)行結(jié)果:(7)適配器模式現(xiàn)有一個(gè)接口DataOperation定義了排序方法sort(int[])和查找方法search(int[],int),已知類QuickSortquickSort(int[])方法實(shí)現(xiàn)了快速排序算法,類BinarySearchbinarySearch(int[],int)方法實(shí)現(xiàn)了二分查找算法。現(xiàn)使用適配器模式設(shè)計(jì)一個(gè)系統(tǒng),在不修改源代碼情況下將類QuickSort和類BinarySearch方法適配到DataOperation接口中。繪制類圖并編程實(shí)現(xiàn)。(要求實(shí)現(xiàn)快速排序和二分查找)interfaceDataOperation{ voidsort(int[]list); intsearch(int[]list,intnumber);}classQuickSort{ publicint[]quickSort(int[]num){ returnsort(num,0,num.length-1); } privateint[]sort(int[]num,intleft,intright){ if(left<right){ //將第一個(gè)數(shù)作為參考軸 ints=num[left]; inti=left; intj=right+1; while(true){ //從左向右找,直到找到比S大數(shù) while(i+1<num.length&&num[++i]<s); //從右向左找,直到找到比S小數(shù) while(j-1>-1&&num[--j]>s); //此時(shí)假如i>=j,則說明已交叉,跳出該永久循環(huán) if(i>=j){ break; } //不然假如i<=j,則交換下標(biāo)為i和下標(biāo)為j兩元素值 swap(num,i,j); } //比把S小數(shù)放到下標(biāo)為left處 num[left]=num[j]; //最終將軸S置于比它小和比它大兩組數(shù)之間 num[j]=s; //對(duì)S左右兩側(cè)分別再次使用快速排序 sort(num,left,j-1); sort(num,j+1,right); } returnnum; } privatevoidswap(int[]num,inti,intj){ intt; t=num[i]; num[i]=num[j]; num[j]=t; }}classBinarySearch{ publicintbinarySearch(int[]num,intx){ intlow=0; inthigh=num.length-1; while(low<=high){ //此時(shí)拋出ArrayIndexOutOfBoundsException異常.. intmid=low+(high-low)/2; intmidVal=num[mid]; if(x>midVal){ low=mid+1; }elseif(x<midVal){ high=mid-1; }else{ returnmid; } } return-1;//沒找到元素x }}classDataOperationAdapterimplementsDataOperation{ QuickSortquickSort=newQuickSort(); BinarySearchbinarySearch=newBinarySearch(); @Override publicintsearch(int[]list,intnumber){ returnbinarySearch.binarySearch(list,number); } @Override publicvoidsort(int[]list){ quickSort.quickSort(list); } }classDataOperationClient{ publicstaticvoidmain(Stringa[]) { DataOperationoperation=(DataOperation)XMLUtil.getBean(); int[]list={1,3,56,23,54,86,43,57,88,56,82,90}; System.out.println("Listbeforesort:"); for(inti=0;i<list.length;i++){ System.out.print(list[i]+""); } operation.sort(list); System.out.println("\nListaftersort:"); for(inti=0;i<list.length;i++){ System.out.print(list[i]+""); } intnumber=43; intindex=operation.search(list,number); System.out.println("\nSearchfor:"+number+",Positionis:"+index); }}運(yùn)行結(jié)果:(8)橋接模式空客(Airbus)、波音(Boeing)和麥道(McDonnell-Douglas)都是飛機(jī)制造商,它們都生產(chǎn)載客飛機(jī)(PassengerPlane)和載貨飛機(jī)(CargoPlane)?,F(xiàn)在需要設(shè)計(jì)一個(gè)系統(tǒng),描述這些飛機(jī)制造商以及它們所制造飛機(jī)種類,繪制類圖并編程實(shí)現(xiàn)。abstractclassAirPlaneManufacturer{ privateAirPlaneairPlane; publicvoidsetAirPlane(AirPlanenewAirPlane){ this.airPlane=newAirPlane; } abstractvoidproduce();}classAirbusextendsAirPlaneManufacturer{ voidproduce(){ System.out.println("Airbus企業(yè)制造"); }}classBoeingextendsAirPlaneManufacturer{ voidproduce(){ System.out.println("Boeing企業(yè)制造"); }}classMcDonnellDouglasextendsAirPlaneManufacturer{ voidproduce(){ System.out.println("McDonnell-Douglas企業(yè)制造"); }}interfaceAirPlane{ voidfly();}classPassengerPlaneimplementsAirPlane{ publicvoidfly(){ System.out.println("客機(jī)正在飛行!"); }}classCargoPlaneimplementsAirPlane{ publicvoidfly(){ System.out.println("貨機(jī)正在飛行!"); }}classBridgePatternClient{ publicstaticvoidmain(Stringa[]) { AirPlaneairPlane; airPlane=(AirPlane)XMLUtilPen.getBean("plane"); AirPlaneManufacturerplaneFactory; planeFactory=(AirPlaneManufacturer)XMLUtilPen.getBean("factory"); planeFactory.setAirPlane(airPlane); planeFduce(); airPlane.fly(); }}運(yùn)行結(jié)果:(9)組合模式使用組合模式設(shè)計(jì)一個(gè)殺毒軟件(AntiVirus)框架,該軟件既能夠?qū)δ硞€(gè)文件夾(Folder)殺毒,也能夠?qū)δ硞€(gè)指定文件(File)進(jìn)行殺毒,文件種類包含文本文件TextFile、圖片文件ImageFile、音頻視頻文件MediaFile。繪制類圖并編程實(shí)現(xiàn)。interfaceAbstractElement{ publicvoiddisplay();}classImageFileimplementsAbstractElement{ privateStringfilename; publicImageFile(Stringfilename) { this.filename=filename; } publicvoiddisplay() { System.out.println("Imagefile"+filename+"iskillingvirus!"); }}classTextFileimplementsAbstractElement{ privateStringfilename; publicTextFile(Stringfilename) { this.filename=filename; } publicvoiddisplay() { System.out.println("Textfile"+filename+"iskillingvirus!"); }}classMediaFileimplementsAbstractElement{ privateStringfilename; publicMediaFile(Stringfilename) { thi
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學(xué)保健衛(wèi)生室管理制度
- 托教廚房衛(wèi)生管理制度
- 衛(wèi)生間洗浴間管理制度
- 衛(wèi)生清掃及檢查制度
- 鄉(xiāng)鎮(zhèn)衛(wèi)生院急救制度
- 供電所宿舍衛(wèi)生制度
- 衛(wèi)生院貧困人員就診制度
- 榨油加工坊衛(wèi)生管理制度
- 衛(wèi)生系統(tǒng)請(qǐng)銷假制度
- 印刷車間衛(wèi)生間制度
- 復(fù)方蒲公英注射液在銀屑病中的應(yīng)用研究
- 2023屆高考語文二輪復(fù)習(xí):小說標(biāo)題的含義與作用 練習(xí)題(含答案)
- 網(wǎng)絡(luò)直播創(chuàng)業(yè)計(jì)劃書
- 大學(xué)任課老師教學(xué)工作總結(jié)(3篇)
- 3D打印增材制造技術(shù) 課件 【ch01】增材制造中的三維模型及數(shù)據(jù)處理
- 醫(yī)院保潔應(yīng)急預(yù)案
- 化工設(shè)備培訓(xùn)
- 鋼結(jié)構(gòu)安裝施工專項(xiàng)方案
- 高三體育生收心主題班會(huì)課件
- FZ/T 90086-1995紡織機(jī)械與附件下羅拉軸承和有關(guān)尺寸
- 登桿培訓(xùn)材料課件
評(píng)論
0/150
提交評(píng)論