設(shè)計(jì)模式實(shí)驗(yàn)報(bào)告_第1頁
設(shè)計(jì)模式實(shí)驗(yàn)報(bào)告_第2頁
設(shè)計(jì)模式實(shí)驗(yàn)報(bào)告_第3頁
設(shè)計(jì)模式實(shí)驗(yàn)報(bào)告_第4頁
設(shè)計(jì)模式實(shí)驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩29頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

10試驗(yàn)二設(shè)計(jì)模式試驗(yàn)一一、試驗(yàn)?zāi)康腜owerDesigner和任意一種面對對象編程語言實(shí)現(xiàn)幾種常見的設(shè)計(jì)模式構(gòu)造,學(xué)習(xí)如何使用代碼實(shí)現(xiàn)這些模式,并學(xué)會(huì)分析這些模式的使用效果。二、試驗(yàn)內(nèi)容PowerDesigner和任意一種面對對象編程語言實(shí)現(xiàn)簡潔工廠模式、工廠方法模式、抽象工廠模式、單例模式、適配器模式、橋接模式和代碼。面對對象設(shè)計(jì)原則在某圖形庫API中供給了多種矢量圖模板,用戶可以基于這些矢量圖創(chuàng)立不同的顯示圖形,圖形庫設(shè)計(jì)人員設(shè)計(jì)的初始類圖如下所示:ClientCircle Triangle Rectangle+init :void +init :void +init :void+setColor:void +setColor:void +setColor:void+fill :void +fill :void +fill :void+setSize :void +setSize :void +setSize :void+display :void +display :void +display :void... ... ...在該圖形庫中,每個(gè)圖形類〔如Circle、Triangle等〕的init方法用于初始化所創(chuàng)立的圖形,setColorfill方法用于設(shè)置圖形的大小,display方法用于顯示圖形??蛻纛?Client)在使用該圖形庫時(shí)覺察存在如下問題:①由于在創(chuàng)立窗口時(shí)每次只需要使用圖形庫中的一種圖形,因此在更換圖形時(shí)需要修改客戶類源代碼;②在圖形庫中增加并使用的圖形時(shí)需要修改客戶類源代碼;③客戶類在每次使用圖形對象之前需要先創(chuàng)立圖形對象,有些圖形的創(chuàng)立過程較為復(fù)雜,導(dǎo)致客戶類代碼冗長且難以維護(hù)。現(xiàn)需要依據(jù)面對對象設(shè)計(jì)原則對該系統(tǒng)進(jìn)展重構(gòu),要求如下:①隔離圖形的創(chuàng)立和使用,將圖形的創(chuàng)立過程封裝在特地的類中,客戶類在使用圖形時(shí)無須直接創(chuàng)立圖形對象,甚至不需要關(guān)心具體圖形類類名;②客戶類能夠便利地更換圖形或使用增圖形,無須針對具體圖形類編程,符合開閉原則。繪制重構(gòu)之后的類圖并說明在重構(gòu)過程中所運(yùn)用的面對對象設(shè)計(jì)原則。簡潔工廠模式使用簡潔工廠模式設(shè)計(jì)一個(gè)可以創(chuàng)立不同幾何外形(Shape),如圓形(Circle)、矩形(Rectangle)和三角形(Triangle)等的繪圖工具類,每個(gè)幾何圖形均具有繪制Draw和擦除Erase兩個(gè)方法,要求在繪制不支持的幾何圖形時(shí),拋出一個(gè)UnsupportedShapeException特別,繪制類圖并編程模擬實(shí)現(xiàn)。工廠方法模式現(xiàn)需要設(shè)計(jì)一個(gè)程序來讀取多種不同類型的圖片格式圖片讀取器(ImageReader)GIF圖片讀取器(GifReader)用于讀取GIFJPG圖(JpgReader)用于讀取JPG格式的圖片。圖片讀取器對象通過圖片讀取器工廠ImageReaderFactory來創(chuàng)立,ImageReaderFactory是一個(gè)抽象類,用于定義創(chuàng)立圖片讀取器GifReaderFactory和JpgReaderFactory用于創(chuàng)立具體的圖片讀取器對象。試使用工廠方法模式設(shè)計(jì)該程序,繪制類圖并編程模擬實(shí)現(xiàn)。抽象工廠模式淺綠色按鈕Spring風(fēng)格淺綠色按鈕Spring風(fēng)格綠色邊框文本框綠色邊框組合框皮膚庫淺藍(lán)色按鈕Summer風(fēng)格藍(lán)色邊框文本框藍(lán)色邊框組合框圖并編程模擬實(shí)現(xiàn)。單例模式個(gè)或三個(gè),設(shè)計(jì)并編寫代碼實(shí)現(xiàn)一個(gè)多例類。單例模式〔JavaAWT/Swing開發(fā)中可使用JDesktopPane和JInternalFrame來實(shí)現(xiàn),要求在主窗體中某個(gè)內(nèi)部子窗體只能實(shí)例化一次,即只能彈出一個(gè)一樣的子窗體,如以下圖所示,編程實(shí)現(xiàn)該功能。〔注:用C#C++實(shí)現(xiàn)類似功能也可以〕適配器模式現(xiàn)有一個(gè)接口DataOperation定義了排序方法sort(int[])和查找方法search(int[],int),類QuickSortquickSort(int[])BinarySearch的binarySearch(int[],int)下將類QuickSort和類BinarySearch的方法適配到DataOperation〔要求實(shí)現(xiàn)快速排序和二分查找〕適配器模式WindowsMediaPlayerRealPlayer是兩種常用的媒體播放器,它們的API構(gòu)造和調(diào)用API,而且在將來可能還需要支持?jǐn)M實(shí)現(xiàn)。橋接模式某軟件公司欲開發(fā)一個(gè)跨平臺(tái)圖像掃瞄系統(tǒng),要求該系統(tǒng)能夠顯示BMP、JPG、GIF、PNG等多種格式的文件,并且能夠在Windows、Linux、Unix等多個(gè)操作系統(tǒng)上運(yùn)行。系統(tǒng)首先將各種格式的文件解析為像素矩陣(Matrix),然后將像素矩陣顯示在屏幕上,在不同的編程模擬實(shí)現(xiàn)。組合模式北京總部教務(wù)辦公室北京總部教務(wù)辦公室湖南分校行政辦公室教務(wù)辦公室長沙教學(xué)點(diǎn)湘潭教學(xué)點(diǎn)行政辦公室教務(wù)辦公室行政辦公室教務(wù)辦公室行政辦公室在該教育機(jī)構(gòu)的OA組織構(gòu)造,繪制相應(yīng)的類圖并編程模擬實(shí)現(xiàn),在客戶端代碼中模擬下發(fā)公文。三、試驗(yàn)要求選擇適宜的面對對象設(shè)計(jì)原則對系統(tǒng)進(jìn)展重構(gòu),正確無誤繪制重構(gòu)之后的類圖;正確無誤繪制簡潔工廠模式、工廠方法模式、抽象工廠模式、單例模式、適配器模式、橋接模式和組合模式實(shí)例的模式構(gòu)造圖;單例模式、適配器模式、橋接模式和組合模式實(shí)例,代碼運(yùn)行正確無誤。四、試驗(yàn)步驟PowerDesigner繪制重構(gòu)之后的類圖;使用PowerDesigner繪制簡潔工廠模式實(shí)例構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式實(shí)例;使用PowerDesigner繪制工廠方法模式實(shí)例構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式實(shí)例;使用PowerDesigner繪制抽象工廠模式實(shí)例構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式實(shí)例;使用PowerDesigner繪制單例模式實(shí)例構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式實(shí)例;使用PowerDesigner繪制適配器模式構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式;使用PowerDesigner繪制橋接模式構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式;使用PowerDesigner繪制組合模式構(gòu)造圖并用面對對象編程語言實(shí)現(xiàn)該模式。五、試驗(yàn)報(bào)告要求供給重構(gòu)之后的類圖,簡要說明重構(gòu)過程中所運(yùn)用的面對對象設(shè)計(jì)原則;供給簡潔工廠模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;供給工廠方法模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;供給抽象工廠模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;供給單例模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;供給適配器模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;供給橋接模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;供給組合模式實(shí)例構(gòu)造圖及實(shí)現(xiàn)代碼;留意調(diào)整類圖中文字的字體及字號,留意圖的整體布局和圖的大小,避開使用穿插線,留意代碼字體和顏色,代碼字體需全都,顏色需統(tǒng)一;認(rèn)真寫好試驗(yàn)總結(jié);姓名試驗(yàn)2.do060102張三試驗(yàn)2.do全部材料〔包括類圖和源代碼〕均放在一個(gè)Word文件中,格式為docdocx,不需要提交PowerDeigner所保存的oom模型,也無須單獨(dú)提交源代碼。六、試驗(yàn)結(jié)果重構(gòu)之后的類圖:ClientShape+init :void+fill :voidShapeFactory...

+setSize:void+display:void...RectangleFactory

TriangleFactory

CircleFactory

Circle

+init

:void

Traingle...

...

...

<<create>>: : :returnnew

+fill :void+setSize:void

+init :void

RectanglereturnnewRectangle

returnnewTriangle;

Circle;

+display:void...

+fill :void+setSize:void

+init

:void; <<create>>

+display:void

...

+fill

:void<<create>>

+setSize:void+display:void...重構(gòu)過程中所使用的面對對象設(shè)計(jì)原則及簡要說明:重構(gòu)之后實(shí)現(xiàn)了開閉原則:在增加一個(gè)圖形時(shí),只需要增加一個(gè)圖形類和一個(gè)圖形的工廠類,然后將圖形的工廠類放入配置文件,從而使增加圖形時(shí)不用修改源代碼,完全還實(shí)現(xiàn)了單一職責(zé)原則:原先的客戶端類,需要?jiǎng)?chuàng)立具體的用戶對象,擔(dān)當(dāng)了過多的職了單一職責(zé)原則;另外還實(shí)現(xiàn)了依靠倒轉(zhuǎn)原則:客戶端完全只針對抽象編程,沒有依靠于具體的細(xì)節(jié),只需要針對ShapeFactoryShape編程;類圖:ClientShape+Draw:void+Erase:void...Circle+Draw:void+Erase:void...

Triangle+Draw:void+Erase:void...

Rectangle+Draw:void+Erase:void...if(arg.equalsIgnoreCase(“Circle“)){}else

returnnewCircle;if(arg.equalsIgnoreCase(“Triangle“)){

ShapeFactory}else{

returnnewTraingel;returnnewRectangle;

+produceShape(Stringarg):Shape...實(shí)現(xiàn)代碼://代碼顏色使用黑色,字體使用TimesNewRoman或Arial,字號為五號,抽象產(chǎn)品類:ShapepublicinterfaceShape{publicvoiddraw;publicvoiderase;}具體產(chǎn)品類:CirclepublicclassCircleimplementsShape{publicvoiddraw{“drawCircle. “);}publicvoiderase{“eraseCircle!“);}}具體產(chǎn)品類:TrianglepublicclassTriangleimplementsShape{publicvoiddraw{“drawTriangle. “);}publicvoiderase{“eraseTriangle!“);}}具體產(chǎn)品類:RectanglepublicclassRectangleimplementsShape{publicvoiddraw{“drawRectangle......“); publicvoiderase{“eraseRectangle!“);}}工廠類:ShapeFactorypublicclassShapeFactory{publicstaticShapeproduceShape(Stringshape)throwsException{if(shape.equalsIgnoreCase(“Circle“)){“生產(chǎn)一個(gè)Circle對象!“);returnnewCircle;}elseif(shape.equalsIgnoreCase(“Triangle“)){“生產(chǎn)一個(gè)Triangle對象!“);returnnewTriangle;}elseif(shape.equalsIgnoreCase(“Rectangle“)){“生產(chǎn)一個(gè)Rectangle對象!“);returnnewRectangle;}else{thrownewException(“UnsupportedShapeException!“);}}}XML操作工具類:XMLUtilimport;publicclassXMLUtil{publicstaticStringgetClassName{import;import;import;import;import;try{

DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=dFactory.newDocumentBuilder;Documentdoc;doc=builder.parse(newFile(“src/com/config.xml“));NodeListnl=doc.getElementsByTagName(“className“);NodeclassNode=nl.item(0).getFirstChild;StringclassName=classNode.getNodeValue.trim;returnclassName;}catch(Exceptione){e.printStackTrace;returnnull;}}}配置文件:config.xml<?xmlversion=“1.0“?><config><className>Triangle</className></config></config>(8)客戶端測試類:ClientpublicclassClient{publicstaticvoidmain(String[]args){try{Shapeshape;StringclassName=XMLUtil.getClassName;shape=ShapeFduceShape(className);shape.draw;shape.erase;}catch(Exceptione){;}}}ClientJifReaderFactoryClientJifReaderFactoryJifReader<<create>>+produceImageReader:ImageReader...+readImage:void...ImageReaderFactoryImageReader:returnnewJpgReader;+produceImageReader:ImageReader...+readImage:void...JpgReaderFactory<<create>>JpgReader+produceImageReader:ImageReader...+readImage:void...:returnnewJpgReader;抽象產(chǎn)品類:ImageReaderFactory抽象產(chǎn)品類:ImageReaderFactorypublicinterfaceImageReaderpublicvoidreadImage;}具體產(chǎn)品類:JpgReaderpublicclassJpgReaderimplementsImageReader{publicvoidreadImage{“readjgpiamge. “);}}具體產(chǎn)品類:JifReaderpublicclassJifReaderimplementsImageReader{publicvoidreadImage{“readjifiamge. “);}}抽象工廠類:ImageReaderFactorypublicinterfaceImageReaderFactory{publicImageReaderproduceImageReader;}具體工廠類:JpgReaderFactorypublicclassJpgReaderFactoryimplementsImageReaderFactory{publicImageReaderproduceImageReader{“生產(chǎn)一個(gè)jpgreader. “);returnnewJpgReader;}}具體工廠類:JifReaderFactorypublicclassJifReaderFactoryimplementsImageReaderFactory{publicImageReaderproduceImageReader{“生產(chǎn)一個(gè)jifreader. “);returnnewJifReader;}}XML操作工具類:XMLUtilimport;import;import;import;import;import;publicclassXMLUtil{publicstaticObjectgetBean{try{DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=dFactory.newDocumentBuilder;Documentdoc;doc=builder.parse(newFile(“src/config.xml“));NodeListnl=doc.getElementsByTagName(“className“);NodeclassNode=nl.item(0).getFirstChild;StringcName=classNode.getNodeValue;Classc=Class.forName(cName);Objectobj=c.newInstance;returnobj;}catch(Exceptione){e.printStackTrace;!“);returnnull;}}}配置文件:config.xml<?xmlversion=“1.0“?><config><className>JpgReaderFactory</className></config>客戶端測試類:ClientpublicclassClient{publicstaticvoidmain(Stringargs[]){try{ImageReaderimageReader;ImageReaderFactoryimageReaderFactory;imageReaderFactory=(ImageReaderFactory)XMLUtil.getBean;imageReaderimageReader=imageReaderFduceImageReader;imageReader.readImage;}catch(Exceptione){;}}}類圖:ClientClientAbstractButton+action:void...SkinFactoryGreenButtonBlueButton+createButton :AbstractButton+createText :AbstractText+createCombox:AbstractCombox...+action:void...+action:void...AbstractTextSpringSkinFactory+action:void...+createButton :AbstractButton+createText :AbstractText+createCombox:AbstractCombox...GreenTextBlueText+action:void...+action:void...SummerSkinFactoryAbstractCombox+createButton :AbstractButton+createText :AbstractText+action:void...+createCombox:AbstractCombox...GreenComboxBlueCombox+action:void...+action:void...(1)抽象產(chǎn)品類:(1)抽象產(chǎn)品類:AbstractButtonpublicinterfaceAbstractButton{publicvoidaction;}具體產(chǎn)品類:GreenButtonpublicclassGreenButtonimplementsAbstractButton{publicvoidaction{“Spring風(fēng)格的greenbutton...“);}}具體產(chǎn)品類:BlueButtonpublicclassBlueButtonimplementsAbstractButton{publicvoidaction{“Summer風(fēng)格的bluebutton...“);}}抽象產(chǎn)品類:AbstractTextpublicinterfaceAbstractText{publicvoidaction;}具體產(chǎn)品類:GreenTextpublicclassGreenTextimplementsAbstractText{publicvoidaction{“Spring風(fēng)格的greentext...“);}}具體產(chǎn)品類:BlueTextpublicclassBlueTextimplementsAbstractText{publicvoidaction{“Summer風(fēng)格的bluetext...“);}}抽象產(chǎn)品類:AbstractComboxpublicinterfaceAbstractCombox{publicvoidaction;}具體產(chǎn)品類:GreenComboxpublicclassGreenComboximplementsAbstractCombox{publicvoidaction{“Spring風(fēng)格的greencombox...“);}}具體產(chǎn)品類:BlueComboxpublicclassBlueComboximplementsAbstractCombox{publicvoidaction{“Summer風(fēng)格的bluecombox...“);}}抽象工廠類:SkinFactorypublicinterfaceSkinFactory{publicAbstractButtoncreateButton;publicAbstractTextcreateText;publicAbstractComboxcreateCombox;}具體工廠類:SpringSkinFactorypublicclassSpringSkinFactoryimplementsSkinFactory{publicAbstractButtoncreateButton{“SpringSkinFactory生產(chǎn)greenbutton...“);returnnewGreenButton;}publicAbstractTextcreateText{“SpringSkinFactory生產(chǎn)greentext...“);returnnewGreenText;}publicAbstractComboxcreateCombox{“SpringSkinFactory生產(chǎn)greencombox...“);returnnewGreenCombox;}}具體工廠類:SummerSkinFactorypublicclassSummerSkinFactoryimplementsSkinFactory{publicAbstractButtoncreateButton{“SummerSkinFactory生產(chǎn)bluebutton...“);returnnewBlueButton;}publicAbstractTextcreateText{“SummerSkinFactory生產(chǎn)bluetext...“);returnnewBlueText;}publicAbstractComboxcreateCombox{“SummerSkinFactory生產(chǎn)bluecombox...“);returnnewBlueCombox;}}配置文件:config.xml<?xmlversion=“1.0“?><config><className>SpringSkinFactory</className></config>XML操作工具類:XMLUtil(與第三題中的XMLUtil一樣)(15)客戶端測試類:ClientpublicclassClient{publicstaticvoidmain(Stringargs[]){try{SkinFactorysFactory;AbstractButtonbutton;AbstractTexttext;AbstractComboxcombox;sFactory=(SkinFactory)XMLUtil.getBean;button=sFactory.createButton;text=sFactory.createText;combox=sFactory.createCombox;button.action;text.action;combox.action;}catch(Exceptione){;}}}}類圖:0..20..2ClientPrinterinstance1:Printer =nullinstance2:Printer =nullNationality+getInstance:Printer...if(no==1){if(instance1==null)instance1=newPrinter;returninstance1;}if(no==2){if(instance2==null)instance2=newPrinter;returninstance2;}(1)多例類:(1)多例類:PrinterpublicclassPrinter{privatevolatilestaticPrinterinstance1=null;privatevolatilestaticPrinterinstance2=null;publicPrinter{}publicstaticPrintergetInstance(intno){if(no==1){if(instance1==null){synchronized(Printer.class){if(instance1==null){“創(chuàng)立第一臺(tái)打印機(jī) “);instance1=newPrinter;}}}“調(diào)用第一臺(tái)打印機(jī) “);returninstance1;}elseif(no==2){if(instance2==null){synchronized(Printer.class){if(instance2==null){“創(chuàng)立其次臺(tái)打印機(jī) “);instance2=newPrinter;}}}“調(diào)用其次臺(tái)打印機(jī) “);returninstance2;}else{!“);}returnnull;}}客戶端測試類:ClientpublicclassClient{publicstaticvoidmain(Stringargs[]){Printerp1,p2;p1=Printer.getInstance(1);p2=Printer.getInstance(2);p1=Printer.getInstance(1);p2=Printer.getInstance(2);}}類圖:ClientClientinternalFrameJInternalFrameinternalFrame:JInternalFramedesktopPane :JDesktopPane+actionPerformed:JInternalFrame...createsif(internalFrame==null){JInternalFrame;}(1)單例類:(1)單例類:JinternalFrameimport;import;import;import;import;import;import;import;import;import;import;import;publicclassJInternalFrameDemolextendsJFrameimplementsActionListener{JDesktopPanedesktopPane;JInternalFrameinternalFrame;publicJInternalFrameDemol{super(“JInternalFrameDemo1“);ContainercontentPane=this.getContentPane;contentPane.setLayout(newBorderLayout);JButtonbutton=newJButton(“CrateNewInternalFrames“);button.addActionListener(this);contentPane.add(button,BorderLayout.SOUTH);desktopPane=newJDesktopPane;contentPane.add(desktopPane);this.setSize(newDimension(300,300));this.setVisible(true);this.addWindowListener(newWindowAdapter{@OverridepublicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicvoidactionPerformed(ActionEvente){if(internalFrame==null){internalFrame=newJInternalFrame(“InternaleFrame“,true,true,true,true);internalFrame.setLocation(20,20);internalFrame.setSize(newDimension(200,200));internalFrame.setVisible(true);ContainericontentPane=internalFrame.getContentPane;JTextAreatextArea=newJTextArea;JButtonb=newJButton(“InternaleFrameButton“);icontentPane.add(textArea,“Center“);icontentPane.add(b,“South“);desktopPane.add(internalFrame);//this.getContentPane.add(internalFrame);try{internalFrame.setSelected(true);}catch(ex){“Exceptionwhileselecting“);}}}}}(2)客戶端類:ClientpublicclassClient{publicstaticvoidmain(Stringargs[]){newJInternalFrameDemol;}}類圖:Client

DataOperation+sort(intint[]) :void+search(intint[],intno) :intBinarySearchquicksort.quickSort; QBAdapterbsearch :BinarySearchquicksort:QuickSort+sort(intint[]) :void+search(intint[],intno) :intbsearch.binarySearch;實(shí)現(xiàn)代碼:

+binarySearch(intint[],intno):int...QuickSort+quickSort(intint[]) :void...目標(biāo)類:目標(biāo)類:DataOperationpublicclassDataOperation{publicintsearch(intlist[],intno){return0;}publicvoidsort(intlist[]){}}適配器類:QBAdapterpublicclassQBAdapterextendsDataOperation{privateQuickSortqs;privateBinarySearchbs;publicQBAdapter{qs=newQuickSort;bs=newBinarySearch;}publicvoidsort(intlist[]){“調(diào)用QuickSort中的quickSort方法 “);qs.quickSort(list);}publicintsearch(intlist[],intno){intresult;“調(diào)用BinartSearch中的binarySearch方法 “);result=bs.binarySearch(list,no);returnresult;}}適配者類:QuickSortpublicclassQuickSort{publicQuickSort{}publicvoidquickSort(intlist[]){intsize=list.length;quickSortMethod(list,0,size-1);}privatevoidquickSortMethod(intlist[],intleft,intright){intq;if(left<right){q=partition(list,left,right);quickSortMethod(list,left,q-1);quickSortMethod(list,q+1,right);}}privateintpartition(intlist[],intleft,intright){intshaft=list[left];inttemp=0;while(left<right){while(list[right]>shaft)right--;while(list[left]<shaft)left++;if(list[left]==list[right]){right--;}else{temp=list[left];list[left]=list[right];list[right]=temp;}}list[left]=shaft;returnleft;}}適配者類:BinarySearchpublicclassBinarySearch{publicintbinarySearch(intlist[],intno){intleft=0;intright=list.length;try{while(left<=right){intmiddle=(left+right)/2;if(list[middle]==no)returnmiddle;elseif(list[middle]>no)right=middle-1;else}

left=middle+1;}catch(Exceptione){“沒有找到可以匹配的值 “);}return-1;}}XML文件操作工具類:XMLUtilimport;import;import;import;import;import;publicclassXMLUtil{publicstaticObjectgetBean{try{DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=dFactory.newDocumentBuilder;Documentdoc;doc=builder.parse(newFile(“src/config.xml“));NodeListnl=doc.getElementsByTagName(“className“);NodeclassNode=nl.item(0).getFirstChild;StringcName=classNode.getNodeValue;Classc=Class.forName(cName);Objectobj=c.newInstance;returnobj;}catch(Exceptione){e.printStackTrace;!“);returnnull;}}}配置文件:config.xml<?xmlversion=“1.0“?><config><className>QBAdapter</className></config>(7)(7)客戶端測試類:ClientpublicclassClient{publicstaticvoidmain(Stringargs[]){intlist[]={5,3,7,6,2,1,9,8,4,5,5,5};DataOperationdataoperation;dataoperation=(DataOperation)XMLUtil.getBean;dataoperation.sort(list);for(inti=0;i<list.length;i++){+“ “);};“l(fā)ist[“+dataoperation.search(list,5)+“]=5“);}}類圖:Client Player+playMedia(FilefileName):void...:windowsMediaPlayer.play

WindowsMediaPlayerAdapter-windowsMediaPalyer:WindowsMediaPlayer+playMedia(FilefileName):void...windowsMediaPlayer

:realPlayer.play;

RealPlayerAdapter-realPlayer:RealPlayer+playMedia(FilefileName):void...realPlayerWindowsMediaPlayer+play(FilefileName):void...

RealPlayer+play(FilefileName):void...實(shí)現(xiàn)代碼:目標(biāo)抽象類:Playerimport;publicclassPlayer{publicvoidplayMedia(FilefileURL){“nonono“);}}適配器類:RealPlayerAdapterimport;publicclassRealPlayerAdapterextendsPlayer{privateRealPlayerrealPlayer;publicRealPlayerAdapter{realPlayer=newRealPlayer;}publicvoidplayMedia(FilefileURL){“選擇了RealPlayer適配器 “);realPlayer.play(fileURL);}}適配器類:WindowsMediaPlayerAdapterimport;publicclassWindowsMediaPlayerAdapterextendsPlayer{privateWindowsMediaPlayerwindowsMediaPlayer;publicWindowsMediaPlayerAdapter{windowsMediaPlayer=newWindowsMediaPlayer;}publicvoidplayMedia(FilefileURL){“選擇了WindowsMediaPlayer適配器 “);windowsMediaPlayer.play(fileURL);}}適配者類:RealPlayerimport;publicclassRealPlayer{publicvoidplay(FilefileURL){“RealPlayer中的API...“);“開頭播放“+fileURL.getName);}}適配者類:WindowsMediaPlayerimport;publicclassWindowsMediaPlayer{publicvoidplay(FilefileURL){“WindowsMediaPlayer中的API...“);“開頭播放“+fileURL.getName);}}XML文件操作工具類:XMLUtilimport;import;import;import;import;import;publicclassXMLUtil{publicstaticObjectgetBean{try{DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=dFactory.newDocumentBuilder;Documentdoc;doc=builder.parse(newFile(“src/config.xml“));NodeListnl=doc.getElementsByTagName(“className“);NodeclassNode=nl.item(0).getFirstChild;StringcName=classNode.getNodeValue;Classc=Class.forName(cName);Objectobj=c.newInstance;returnobj;}catch(Exceptione){e.printStackTrace;e.printStackTrace;returnnull;}}}配置文件:config.xml<?xmlversion=“1.0“?><config><className>WindowsMediaPlayerAdapter</className></config>客戶端測試類:Clientimport;publicclassClient{publicstaticvoidmain(Stringargs[]){FilefileURL=newFile(“F:/test.rmvb“);Playerplayer;player=(Player)XMLUtil.getBean;player.playMedia(fileURL);}}ClientBMPImageClientBMPImage+parseImage:void...JPGImageImageBrowsingSystem{abstract}Image-image:Image+paintImage:voidimage+setImage...:void+parseImage:void...+parseImage:void...GIFImage+parseImage:void...PNGImageWindowsSystemUnixSystem+paintImage:void+paintImage:void+parseImage:void...LinuxSysgtem+paintImage:void實(shí)現(xiàn)代碼:實(shí)現(xiàn)類接口:ImagepublicinterfaceImage{publicvoidparseImage;}具體實(shí)現(xiàn)類:BMPImagepublicclassBMPImageimplementsImage{publicvoidparseImage{“解析bmp圖像 “);}}具體實(shí)現(xiàn)類:JIFImagepublicclassJIFImageimplementsImage{publicvoidparseImage{“解析jif圖像 “);}}具體實(shí)現(xiàn)類:JPGImagepublicclassJPGImageimplementsImage{publicvoidparseImage{“解析jpg圖像 “);}}具體實(shí)現(xiàn)類:PNGImagepublicclassPNGImageimplementsImage{publicvoidparseImage{“解析png圖像 “);}}抽象類:ImageBrowsingSystempublicabstractclassImageBrowsingSystem{privateImageimage;publicImageBrowsingSystem{}publicvoidsetImage(Imageimage){this.image=image;}publicImagegetImage{returnthis.image;}publicabstractvoidpaintImage;}擴(kuò)大抽象類:LinuxSystempublicclassLinuxSystemextendsImageBrowsingSystem{publicvoidpaintImage{“調(diào)用linuxsystem中的函數(shù)來繪制像素矩陣 “);}}擴(kuò)大抽象類:LinuxSystempublicclassUnixSystemextendsImageBrowsingSystem{publicvoidpaintImage{“調(diào)用unixsystem中的函數(shù)來繪制像素矩陣 “);}}擴(kuò)大抽象類:WindowsSystempublicclassWindowsSystemextendsImageBrowsingSystem{publicvoidpaintImage{“調(diào)用windowssystem中的函數(shù)來繪制像素矩陣 “);}}XML文件操作類:XMLUtilimport;import;import;import;import;import;publicclassXMLUtil{publicstaticObjectgetBean(Stringargs){try{DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=dFactory.newDocumentBuilder;Documentdoc;doc=builder.parse(newFile(“src/config.xml“));NodeListnl=null;NodeclassNode=null;StringcName=null;nl=doc.getElementsByTagName(“className“);if(args.equals(“image“)){classNode=nl.item(0).getFirstChild;}elseif(args.equals(“imageBrowsingSystem“)){classNode=nl.item(1).getFirstChild;}cName=classNode.getNodeValue;Classc=Class.forName(cName);Objectobj=c.newInstance;returnobj;}catch(Exceptione){e.printStackTrace;!“);returnnull;}}}配置文件:config.xml<?xmlversion=“1.0“?><config><className>JIFImage</className><className>UnixSystem</className></config>客戶端測試類:ClientpublicclassClient{publicstaticvoidmain(Stringargs[]){Imageimage;ImageBrowsingSystemimageBrowsingSystem;imageimage=(Image)XMLUtil.getBean(“image“);imageBrowsingSystem(ImageBrowsingSystem)XMLUtil.getBean(“imageBrowsingSystem“);imageBrowsingSystem.setImage(image);imageBrowsingSystem.getImage.parseImage;imageBrowsingSystem.paintImage;}}=ClientAbstractHeadClientAbstractHead{abstract}+receiveDocument:voidProvostOff

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論