版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
privateMultiton(){publicstaticMultitongetInstance(){//TODO:implementif(multitonList.size()<NUMBER){System.out.println("创建新实例成功");Multitoncurrent=newMultiton();multitonList.add(current);returncurrent;}else{System.out.println("不能创建更多的实例");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));
运行结果:booledn运行结果: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("创建一个子窗体");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.类图:实现代码:目标抽象类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)〃如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较end--;if(a[end]<=key){inttemp=a[end];a[end]=a[start];a[start]=temp;}〃从前往后比较while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置start++;if(a[start]>=key){inttemp=a[start];a[start]=a[end];a[end]=temp;}〃此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用}〃递归if(start>low)quickSout(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1if(end<high)4山©女5。a3,0口4+1,出8卜);〃右边序列ij。从关键值索引+1到最后一个}}适配者类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("也"+"实现快速排序:");for(inti=0;iva.length;i++){System.out.print(a[i]+"");}System.out.println("也"+"实现了二分查找算法,查找“20”");System.out.println(dataOperation.search(a,0,a.length-1,20));}}8.类图:实现代码:抽象类Productor:packageBridgePattern;publicabstractclassProductor{protectedPlaneplane;publicvoidsetPlane(Planeplane){this.plane=plane;}publicabstractvoidproduct。;}扩充抽象类AirbusPro:packageBridgePattern;publicclassAirbusProextendsProductor{©Overridepublicvoidproduct(){System.out.println("Airbuscompary");plane.createPlane();}}扩充抽象类McDonnellPro:packageBridgePattern;publicclassMcDonnellProextendsProductor{©Overridepublicvoidproduct(){System.out.println("McDonnellcompany");plane.createPlane();}}扩充抽象类BoeingPro:packageBridgePattern;publicclassBoeingProextendsProductor{©Overridepublicvoidproduct。{System.out.println("Boeingcompany");plane.createPlane();实现类接口Plane:packageBridgePattern;publicinterfacePlane{publicvoidcreatePlane();}具体实现类PassengerPlane;packageBridgePattern;publicclassPassengerPlaneimplementsPlane{©OverridepublicvoidcreatePlane(){System.out.println("bulidaPassengerPlane");}具体实现类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模式:组合模式定义:组合多个对象形成树型结构以表示具有部分0n3me Siring模式:组合模式定义:组合多个对象形成树型结构以表示具有部分-整体关系的层次结构。组合模式让客户端可以统一对待单个对象和组合对象。CompositePattern:Composeobjectsintotreestructurestorepresentpart-wholehierarchies.Compositeletsclientstreatindividualobjectandcompositionsofobjectuniformly.实现代码:抽象构件类Component:packageCompositePattern;sharsfCamponent) vmd俞,onMessagefl voidpublicabstractclassComponent{publicabstractvoidadd(Componentccomponent);publicabstractvoidremove(Componentcomponent);publicabstractvoidshare(Componentcomponent);}叶子构件类Member:packageCompositePattern;importjava.util.ArrayList;publicclassMemberextendsComponent{publicMember(Stringname){=name;publicStringgetName(){returnname;publicvoidsetName(Stringname){=name;privateStringname;©Overridepublicvoidadd(Componentccomponent){System.out.println("调这个函数也没用");©Overridepublicvoidremove(Componentcomponent){System.out.println("调这个函数也没用");©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()+"收到消息");}容器构件类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()+”分享了一条新动态");member1.share(group1);System.out.println(" ");Componentgroup2=newGroup("group2");group2.add(group1);Componentmember5=newMember("member5");group2.add(member5);System.out.println(((Member)member5).getName()+”分享了一条新动态");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实现代码:抽象构件类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()+"接收公文");}}容器构件类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()+"发送公文");for(OrganizationComponentoc:organizationCompoent){if(ocinstanceofDeptComponent){oc.sendMessage(msg);}elseif(ocinstanceofOffice){oc.onMessage();}publicvoidonMessage(){System.out.println("错误方法");}客户端类Client:packageCompositePattern2;publicclassClient{publicstaticvoidmain(String口args){OrganizationComponentbeijing=newDeptComponent();beijing.setName("北京总部");OrganizationComponentjiaowuOfficel=newOffice();jiaowuOffice1.setName("教务办公室1");OrganizationComponenthunanfenxiao=newDeptComponent();hunanfenxiao.setName("湖南分校");OrganizationComponentxingzhengOffice=newOf
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 茶园租赁合同:茶树种植研究合作
- 城市宣传编剧合作合同
- 石油开采电力供应施工合同
- 游泳池地坪漆施工合同
- 2024年度防水材料行业市场调研与竞争分析合同3篇
- 水土保持HSE安全合同
- 2024年新版商品房买卖合同:专业与规范3篇
- 幼儿园卫生保障人员聘用合同
- 2024年度公路工程建设与拆除合同3篇
- 2024年度期刊出版与发行合同3篇
- 说课大赛作品于素玲9-《药物制剂技术》课程设计
- 2025届高三复习备考工作计划暨行动方案
- 三位数乘以三位数-计算题-竖式-50题-
- 医疗器械商贸公司简介
- 《逻辑的力量》课件++2023-2024学年统编版高中语文选择性必修上册
- 内科主任职责
- 搬迁保密工作方案
- 油款欠账合同
- 宝玉石鉴定指南智慧树知到期末考试答案2024年
- 水利工程的历史与发展
- 2023年中国软件行业基准数据SSM-BK-202310
评论
0/150
提交评论