1实验报告系别班级学号姓名时间地点计算机科学20160411课程名称设计模式实验名称抽象工厂模式实验过程一.实验目的与要求1、熟练使用抽象工厂模式二.实验设备微型计算机三.实验原理和实验内容实验原理:提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。1.计算机包含内存(RAM)、CPU等硬件设备,根据下面的“产品等级结构-产品族”示意图,使用抽象工厂模式实现计算机设备创建过程并绘制相应的类图。产品族PcPcCPUPcRAMMacMacCPUMacRAMCPURAM产品等级结构2.某手机游戏软件公司退出一款新的游戏软件,该软件能够支持Symbin,Android等多个主流的手机操作系统平台,针对不同的手机操作系统,该游戏软件提供了不同的游戏操作控制类和游戏界面控制类,并提供相应的工厂来封装这些类的初始化。采用抽象工厂模式进行设计并绘制相应的类图。四.实验结果21.类图及代码:interfaceCPU{voidcreate();}interfaceRAM{voidcreate();}classPcCPUimplementsCPU{publicvoidcreate(){System.out.println(wincpu);}}classMacCPUimplementsCPU{publicvoidcreate(){System.out.println(mac'sCPU);}}classPcRAMimplementsRAM{publicvoidcreate(){System.out.println(winRAM);}}classMacRAMimplementsRAM{publicvoidcreate(){System.out.println(mac'RAM);}}interfaceComputerFactory{CPUproduceCPU();RAMproduceRAM();}classPcFactoryimplementsComputerFactory{publicPcCPUproduceCPU(){System.out.println(CPUover!);3returnnewPcCPU();}publicPcRAMproduceRAM(){System.out.println(RAMover);returnnewPcRAM();}}classMacFactoryimplementsComputerFactory{publicMacCPUproduceCPU(){System.out.println(win7'sCPUhere!);returnnewMacCPU();}publicMacRAMproduceRAM(){System.out.println(win7'sRAMhere!);returnnewMacRAM();}}classComputerPartsClient{publicstaticvoidmain(Stringa[]){ComputerFactoryfactory;CPUcpu;RAMram;Factory=newComputerFactory();cpu=factory.produceCPU();cpu.create();ram=factory.produceRAM();ram.create();}}2.类图及代码:interfaceContral{voidcreate();}interfaceView{voidcreate();}4classAndroidimplementsContral{publicvoidcreate(){System.out.println(contral);}}classAndroid2implementsView{publicvoidcreate(){System.out.println(view);}}classSymbinimplementsContral{publicvoidcreate(){System.out.println(syscontral);}}classSymbin2implementsView{publicvoidcreate(){System.out.println(sysview);}}interfaceFactory{Contralproduce1();Viewproduce2();}classPhoneFactoryimplementsFactory{publicContralproduce1(){System.out.println(over!);returnnewSymbin();}publicViewproduce2(){System.out.println(2over);returnnewAndroid2();}}5classSymbinFactoryimplementsFactory{publicSymbinproduce1(){System.out.println(here!);returnnewSymbin();}publicSymbin2produce2(){System.out.println(here!);returnnewSymbin2();}}classComputerPartsClient{publicstaticvoidmain(Stringa[]){Factoryfactory=newSymbinFactory();Contralc;Viewv;c=factory.produce1();c.create();v=factory.produce2();v.create();}}五.实验总结6