中南大學(xué)設(shè)計(jì)模式試驗(yàn)2_第1頁(yè)
中南大學(xué)設(shè)計(jì)模式試驗(yàn)2_第2頁(yè)
中南大學(xué)設(shè)計(jì)模式試驗(yàn)2_第3頁(yè)
中南大學(xué)設(shè)計(jì)模式試驗(yàn)2_第4頁(yè)
中南大學(xué)設(shè)計(jì)模式試驗(yàn)2_第5頁(yè)
已閱讀5頁(yè),還剩38頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

privateMultiton(){publicstaticMultitongetInstance(){//TODO:implementif(multitonList.size()<NUMBER){System.out.println("創(chuàng)建新實(shí)例成功");Multitoncurrent=newMultiton();multitonList.add(current);returncurrent;}else{System.out.println("不能創(chuàng)建更多的實(shí)例");returnmultitonList.get(newRandom().nextInt(multitonList.size()));}客戶端類Client:packageSingleton;publicclassClient{publicstaticvoidmain(String[]args){Multitonm1=Multiton.getInstance();Multitonm2=Multiton.getInstance();Multitonm3=Multiton.getInstance();Multitonm4=Multiton.getInstance();System.out.println("m1==m2:"+(m1==m2));System.out.println("m2==m3:"+(m2==m4));System.out.println("m3==m4:"+(m3==m4));

運(yùn)行結(jié)果:booledn運(yùn)行結(jié)果:boolednrpublicstaticvoidmain(String口args){System.out.println("HelloWorld!");newJInternalFrameDemo();}}類JInternalFrameDemo:importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Dimension;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjavax.swing.JButton;importjavax.swing.JDesktopPane;importjavax.swing.JFrame;publicclassJInternalFrameDemoextendsJFrameimplementsActionListener{privatestaticJInternalFrame1internalFrame;ContainercontentPane=this.getContentPane();publicJInternalFrameDemo。{super("主窗體)contentPane.setLayout(newBorderLayout());JButtonbutton=newJButton("創(chuàng)建一個(gè)子窗體");button.addActionListener(this);contentPane.add(button,BorderLayout.SOUTH);this.setSize(newDimension(300,300));this.setVisible(true);this.addWindowListener(newWindowAdapter(){?OverridepublicvoidwindowClosing(WindowEvente){System.exit(O);}});}publicvoidactionPerformed(ActionEvente){internalFrame=JInternalFrame1.getJInternalFrame1("子窗體",true,true,true,true);internalFrame.setSize(newDimension(200,200));internalFrame.setVisible(true);JDesktopPanedesktopPane=newJDesktopPane();contentPane.add(desktopPane);desktopPane.add(internalFrame);try{internalFrame.setSelected(true);}catch(java.beans.PropertyVetoExceptionex){System.out.println("Exceptionwhileselecting");}}}類JInternalFrame1:importjavax.swing.JInternalFrame;publicclassJInternalFramelextendsJInternalFrame{privatestaticJInternalFramelJIF1=null;privateJInternalFrame1(Stringname,booleanb1,booleanb2,booleanb3,booleanb4){super(name,b1,b2,b3,b4);}publicstaticJInternalFramelgetJInternalFrame1(Stringname,booleanb1,booleanb2,booleanb3,booleanb4){if(JIF1==null)JIF1=newJInternalFrame1(name,b1,b2,b3,b4);returnJIF1;}}7.類圖:實(shí)現(xiàn)代碼:目標(biāo)抽象類DataOperation:publicinterfaceDataOperation{publicvoidsort(int口a,intlow,inthigh);publicintsearch(int[]srcArray,intstart,intend,intkey);}適配者類QuickSort:publicclassQuickSort{publicvoidquickSout(int口a,intlow,inthigh){intstart=low;intend=high;intkey=a[low];while(end>start){〃從后往前比較while(end>start&&a〔end]>=key)〃如果沒有比關(guān)鍵值小的,比較下一個(gè),直到有比關(guān)鍵值小的交換位置,然后又從前往后比較end--;if(a[end]<=key){inttemp=a[end];a[end]=a[start];a[start]=temp;}〃從前往后比較while(end>start&&a[start]<=key)//如果沒有比關(guān)鍵值大的,比較下一個(gè),直到有比關(guān)鍵值大的交換位置start++;if(a[start]>=key){inttemp=a[start];a[start]=a[end];a[end]=temp;}〃此時(shí)第一次循環(huán)比較結(jié)束,關(guān)鍵值的位置已經(jīng)確定了。左邊的值都比關(guān)鍵值小,右邊的值都比關(guān)鍵值大,但是兩邊的順序還有可能是不一樣的,進(jìn)行下面的遞歸調(diào)用}〃遞歸if(start>low)quickSout(a,low,start-1);//左邊序列。第一個(gè)索引位置到關(guān)鍵值索引-1if(end<high)4山?女5。a3,0口4+1,出8卜);〃右邊序列ij。從關(guān)鍵值索引+1到最后一個(gè)}}適配者類BinarySearch:publicclassBinarySearch{publicintbinarySearch(intsrcArray口,intstart,intend,intkey){intmid=(end-start)/2+start;if(srcArray[mid]==key){returnmid;}if(start>=end){return-1;}elseif(key>srcArray[mid]){returnbinarySearch(srcArray,mid+1,end,key);}elseif(key<srcArray[mid]){returnbinarySearch(srcArray,start,mid-1,key);}return-1;適配器類DataOpAdapter:publicclassDataOpAdapterimplementsDataOperation{privateQuickSortqSort;privateBinarySearchbinarySearch;publicDataOpAdapter(QuickSortqSort,BinarySearchbinarySearch){this.qSort=qSort;this.binarySearch=binarySearch;}?Overridepublicintsearch(int[]srcArray,intstart,intend,intkey){returnbinarySearch.binarySearch(srcArray,start,end,key);}?Overridepublicvoidsort(int口a,intlow,inthigh){qSort.quickSout(a,low,high);}}客戶端類Main:publicclassMain{publicstaticvoidmain(String[]args){DataOperationdataOperation=newDataOpAdapter(newQuickSort(),newBinarySearch。);int口a={12,20,5,16,15,1,30,45,23,9};System.out.println("排序前:");for(inti=0;iva.length;i++){System.out.print(a[i]+"");}intstart=0;intend=a.length-1;dataOperation.sort(a,start,end);System.out.println("也"+"實(shí)現(xiàn)快速排序:");for(inti=0;iva.length;i++){System.out.print(a[i]+"");}System.out.println("也"+"實(shí)現(xiàn)了二分查找算法,查找“20”");System.out.println(dataOperation.search(a,0,a.length-1,20));}}8.類圖:實(shí)現(xiàn)代碼:抽象類Productor:packageBridgePattern;publicabstractclassProductor{protectedPlaneplane;publicvoidsetPlane(Planeplane){this.plane=plane;}publicabstractvoidproduct。;}擴(kuò)充抽象類AirbusPro:packageBridgePattern;publicclassAirbusProextendsProductor{?Overridepublicvoidproduct(){System.out.println("Airbuscompary");plane.createPlane();}}擴(kuò)充抽象類McDonnellPro:packageBridgePattern;publicclassMcDonnellProextendsProductor{?Overridepublicvoidproduct(){System.out.println("McDonnellcompany");plane.createPlane();}}擴(kuò)充抽象類BoeingPro:packageBridgePattern;publicclassBoeingProextendsProductor{?Overridepublicvoidproduct。{System.out.println("Boeingcompany");plane.createPlane();實(shí)現(xiàn)類接口Plane:packageBridgePattern;publicinterfacePlane{publicvoidcreatePlane();}具體實(shí)現(xiàn)類PassengerPlane;packageBridgePattern;publicclassPassengerPlaneimplementsPlane{?OverridepublicvoidcreatePlane(){System.out.println("bulidaPassengerPlane");}具體實(shí)現(xiàn)類CargoPlane;packageBridgePattern;publicclassCargoPlaneimplementsPlane{?OverridepublicvoidcreatePlane(){System.out.println("buildaCargoPlane");}客戶端類Client(沒使用配置文件):packageBridgePattern;publicclassClient{publicstaticvoidmain(String口args){Productorproductor;CargoPlanecargoPlane=newCargoPlane();//PassengerPlanepaplane=newPassengerPlane();productor=newAirbusPro();productor.setPlane(cargoPlane);duct();類圖:唱|■!ComponentTOC\o"1-5"\h\zQ add{Component] void曲 removefCcjnipcnem^ void|即 share(Componerrt} votdQ- Q- addCCompane void循,L remave(Component} va(d?』share{Component} vaid電namt String莓components ArrayUsKCampane-nrt>Os電namt String莓components ArrayUsKCampane-nrt>OsMemberM&mb&r{String)@Cflent0j.imalntStrlngQ)void0■ add(Component} void?0n3me Siring模式:組合模式定義:組合多個(gè)對(duì)象形成樹型結(jié)構(gòu)以表示具有部分0n3me Siring模式:組合模式定義:組合多個(gè)對(duì)象形成樹型結(jié)構(gòu)以表示具有部分-整體關(guān)系的層次結(jié)構(gòu)。組合模式讓客戶端可以統(tǒng)一對(duì)待單個(gè)對(duì)象和組合對(duì)象。CompositePattern:Composeobjectsintotreestructurestorepresentpart-wholehierarchies.Compositeletsclientstreatindividualobjectandcompositionsofobjectuniformly.實(shí)現(xiàn)代碼:抽象構(gòu)件類Component:packageCompositePattern;sharsfCamponent) vmd俞,onMessagefl voidpublicabstractclassComponent{publicabstractvoidadd(Componentccomponent);publicabstractvoidremove(Componentcomponent);publicabstractvoidshare(Componentcomponent);}葉子構(gòu)件類Member:packageCompositePattern;importjava.util.ArrayList;publicclassMemberextendsComponent{publicMember(Stringname){=name;publicStringgetName(){returnname;publicvoidsetName(Stringname){=name;privateStringname;?Overridepublicvoidadd(Componentccomponent){System.out.println("調(diào)這個(gè)函數(shù)也沒用");?Overridepublicvoidremove(Componentcomponent){System.out.println("調(diào)這個(gè)函數(shù)也沒用");?Overridepublicvoidshare(Componentcomponent){if(componentinstanceofGroup){ArrayList<Component>components=((Group)component).getComponents();if(components.contains(this)){components.remove(this);for(Componentobj:components){obj.share(obj);components.add(this);}elseif(componentinstanceofMember){((Member)component).onMessage();publicvoidonMessage(){System.out.println(this.getName()+"收到消息");}容器構(gòu)件類Group:packageCompositePattern;importjava.util.ArrayList;publicclassGroupextendsComponent{publicGroup(Stringname){=name;publicArrayList<Component>getComponents(){returncomponents;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){=name;}privateStringname;publicArrayList<Component>components=newArrayList<>();?Overridepublicvoidadd(Componentccomponent){components.add(ccomponent);}?Overridepublicvoidremove(Componentcomponent){components.remove(component);}?Overridepublicvoidshare(Componentcomponent){if(componentinstanceofGroup){ArrayList<Component>com=((Group)component).getComponents();for(Componentobject:components){object.share(object);}}elseif(componentinstanceofMember){((Member)component).onMessage();}}}客戶端類Client:packageCompositePattern;publicclassClient{publicstaticvoidmain(String口args){Componentgroup1=newGroup("Group1");Componentmember1=newMember("member1");Componentmember2=newMember("member2");Componentmember3=newMember("member3");Componentmember4=newMember("member4");group1.add(member1);group1.add(member2);group1.add(member3);group1.add(member4);System.out.println(((Member)member1).getName()+”分享了一條新動(dòng)態(tài)");member1.share(group1);System.out.println(" ");Componentgroup2=newGroup("group2");group2.add(group1);Componentmember5=newMember("member5");group2.add(member5);System.out.println(((Member)member5).getName()+”分享了一條新動(dòng)態(tài)");member5.share(group2);}}類圖:

□rganizationComponent金\OrgantzaltanComponent新畤tenClEentQ addCompanem{OrgantzatlanComponent)Q remaveCompone叫QrgantzatanCamponem)Organf□rganizationComponent金\OrgantzaltanComponent新畤tenClEentQ addCompanem{OrgantzatlanComponent)Q remaveCompone叫QrgantzatanCamponem)Organf工mGonComponentvoidvoidOrga由attonC□mponentvodvoidmarnfStr^gD}void由『addComponent{OrganfezatlonCamponent} void0bFemoveComponentfOrgantzatlanCamponent]vaEd0getCNFcKtnt)@,sendMessage{String)0onMe5,sage{}通nam&■.:DeptComponentOi:organtzatlanCompoent 5euOrganLzationCamponeFit>Q%getchttd(lnt)?5endMes^ge(5tring)命r--anMessage{)通nameQ:bgetchttdftm}的:sendMessage{Strlng)? onMessage^}電name實(shí)現(xiàn)代碼:抽象構(gòu)件類OrganizationComponent:packageCompositePattern2;publicclassOrganizationComponent{privateStringname;publicStringgetName(){return;一?,.gnew., '""z…'……a”—'”publicOrganizationComponentgetChild(inti){} re'"",iiS,ingmsg)lpublicvoidonMessage(){…。ffice:packageCompositePattern2;publicclassOfficeextendsOrganizationComponent{privateStringname;publicStringgetName(){return;一?,.g.e;publicvoidaddComponent(OrganizationComponentorganizationComponent){}………publicvoidremoveComponent(OrganizationComponentorganizationComponent){}—“publicOrganizationComponentgetChild(inti){System.out.println("errormethod");returnnull;}publicvoidsendMessage(Stringmsg){System.out.println("errormethod");}publicvoidonMessage(){System.out.println(this.getName()+"接收公文");}}容器構(gòu)件類DeptComponent:packageCompositePattern2;importjava.util.HashSet;importjava.util.Set;publicclassDeptComponentextendsOrganizationComponent{publicSet<OrganizationComponent>organizationCompoent=newHashSet<>();privateStringname;publicStringgetName(){return;}publicvoidsetName(StringnewName){=newName;publicvoidaddComponent(OrganizationComponentorganizationComponent){organizationCompoent.add(organizationComponent);}publicvoidremoveComponent(OrganizationComponentorganizationComponent){organizationCompoent.remove(organizationComponent);}publicOrganizationComponentgetChild(inti){System.out.println("獲取子代元素");returnnull;}publicvoidsendMessage(Stringmsg){System.out.println(this.getName()+"發(fā)送公文");for(OrganizationComponentoc:organizationCompoent){if(ocinstanceofDeptComponent){oc.sendMessage(msg);}elseif(ocinstanceofOffice){oc.onMessage();}publicvoidonMessage(){System.out.println("錯(cuò)誤方法");}客戶端類Client:packageCompositePattern2;publicclassClient{publicstaticvoidmain(String口args){OrganizationComponentbeijing=newDeptComponent();beijing.setName("北京總部");OrganizationComponentjiaowuOfficel=newOffice();jiaowuOffice1.setName("教務(wù)辦公室1");OrganizationComponenthunanfenxiao=newDeptComponent();hunanfenxiao.setName("湖南分校");OrganizationComponentxingzhengOffice=newOf

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論