5抽象工厂模式-单例模式答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

(4)抽象工厂模式电脑配件生产工厂生产内存、CPU等硬件设备,这些内存、CPU的品牌、型号并不一定相同,根据下面的“产品等级结构-产品族”示意图,使用抽象工厂模式实现电脑配件生产过程并绘制相应的类图,绘制类图并编程实现。PcCPU+Implementdiscribe():voidMacCPU+Implementdiscribe():voidPcRAM+Implementdiscribe():voidMacRAM+Implementdiscribe():voidCPU+discribe():voidRAM+discribe():voidComputerPartsFactory++produceCPU()produceRAM():CPU:RAMPcFactory++ImplementImplementproduceCPU()produceRAM():CPU:RAMMacFactory++ImplementImplementproduceCPU()produceRAM():CPU:RAMinterfaceCPU{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();//PcORMaccpu=factory.produceCPU();cpu.discribe();ram=factory.produceRAM();ram.discribe();}}(6)单例模式用懒汉式单例实现在某系统运行时,其登录界面类LoginForm只能够弹出一个,如果第二次实例化该类则提示“程序已运行”。绘制类图并编程实现。classLoginFormextendsJFrame{//Java.swing包中的JFrame类用于创建窗口privatestaticLoginFormuniqueInstance=null;privateLoginForm(){//设置标题this.setTitle(LoginForm);this.setSize(400,300);//setSize(intwidth,intheight)//设置关闭按钮的事件this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置是否可见this.setVisible(true);}publicstaticLoginFormgetInstance(){if(uniqueInstance==null){uniqueInstance=newLoginForm();}else{JOptionPane.showMessageDialog(null,LoginFrame已经在运行!);}returnuniqueInstance;}}classLoginFormClient{publicstaticvoidmain(Stringa[]){LoginForms1=null,s2=null;s1=LoginForm.getInstance();s2=LoginForm.getInstance();System.out.println(s1==s2?);System.out.println(s1==s2);LoginForm-singleton:LoginForm-+ConstructorLoginForm()getInstance():LoginForm}}运行结果:

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功