1实验报告课程:设计模式实验学期:2010-2011学年第一学期任课教师:专业:学号:姓名:成绩:实验4抽象工厂模式1.题目:使用抽象工厂模式实现一个DIY电脑店,其中的电脑配置有三个种类:学生版、家庭装、豪华版。2.模式设计的UML类图:23.程序源代码:(1)计算机配置工厂接口ComputerConfigurationFactory.java:publicinterfaceComputerConfigurationFactory{publicCpucreateCpu();publicRamcreateRam();publicMainboardcreateMainboard();publicHarddiskcreateHarddisk();publicDisplaycreateDisplay();}(2)计算机配置工厂接口的各个子类:publicclassStudentComputerConfigurationFactoryimplementsComputerConfigurationFactory{publicCpucreateCpu(){returnnewStudentCpu();}publicRamcreateRam(){returnnewStudentRam();}publicMainboardcreateMainboard(){returnnewStudentMainboard();}publicHarddiskcreateHarddisk(){returnnewStudentHarddisk();}publicDisplaycreateDisplay(){returnnewStudentDisplay();}}publicclassHomeComputerConfigurationFactoryimplementsComputerConfigurationFactory{publicCpucreateCpu(){returnnewHomeCpu();}publicRamcreateRam(){returnnewHomeRam();}publicMainboardcreateMainboard(){returnnewHomeMainboard();}publicHarddiskcreateHarddisk(){returnnewHomeHarddisk();}publicDisplaycreateDisplay(){returnnewHomeDisplay();}}publicclassDeluxeComputerConfigurationFactoryimplementsComputerConfigurationFactory3{publicCpucreateCpu(){returnnewDeluxeCpu();}publicRamcreateRam(){returnnewDeluxeRam();}publicMainboardcreateMainboard(){returnnewDeluxeMainboard();}publicHarddiskcreateHarddisk(){returnnewDeluxeHarddisk();}publicDisplaycreateDisplay(){returnnewDeluxeDisplay();}}(3)电脑各种配置的接口:publicinterfaceCpu{publicStringtoString();}publicinterfaceRam{publicStringtoString();}publicinterfaceMainboard{publicStringtoString();}publicinterfaceHarddisk{publicStringtoString();}publicinterfaceDisplay{publicStringtoString();}(4)电脑配置Cpu接口的各个子类:publicclassStudentCpuimplementsCpu{publicStringtoString(){returnIntel奔腾双核E5400(盒);}}publicclassHomeCpuimplementsCpu{publicStringtoString(){returnIntel酷睿i5650(盒);}}4publicclassDeluxeCpuimplementsCpu{publicStringtoString(){returnIntel酷睿i7980X(至尊版);}}(5)电脑配置Ram接口的各个子类:publicclassStudentRamimplementsRam{publicStringtoString(){return宇瞻2GBDDR31333(经典系列);}}publicclassHomeRamimplementsRam{publicStringtoString(){return金士顿4GBDDR31333(骇客神条套装);}}publicclassDeluxeRamimplementsRam{publicStringtoString(){return海盗船TW3X4G2000C9DF;}}(6)电脑配置Mainboard接口的各个子类:publicclassStudentMainboardimplementsMainboard{publicStringtoString(){return铭瑄MS-M3A890G;}}publicclassHomeMainboardimplementsMainboard{publicStringtoString(){return技嘉GA-870A-UD3;}}publicclassDeluxeMainboardimplementsMainboard{publicStringtoString(){return华硕RampageIIIExtreme;}}(7)电脑配置Harddisk接口的各个子类:publicclassStudentHarddiskimplementsHarddisk{publicStringtoString(){return希捷250GB7200.128M;}}publicclassHomeHarddiskimplementsHarddisk{publicStringtoString(){return三星500GB7200转16M(串/金宝);}}5publicclassDeluxeHarddiskimplementsHarddisk{publicStringtoString(){return希捷Cheetah15K.7600GB;}}(8)电脑配置Display接口的各个子类:publicclassStudentDisplayimplementsDisplay{publicStringtoString(){return飞利浦193E1SB;}}publicclassHomeDisplayimplementsDisplay{publicStringtoString(){return三星P2450H;}}publicclassDeluxeDisplayimplementsDisplay{publicStringtoString(){return戴尔UltraSharp2408WFP;}}(9)抽象产品计算机类Computer.java:publicabstractclassComputer{Stringname;Cpucpu;Ramram;Mainboardmainboard;Harddiskharddisk;Displaydisplay;publicabstractvoidprepare();publicvoidsetName(Stringname){this.name=name;}publicStringgetName(){returnname;}publicStringgetCpu(){returncpu.toString();}publicStringgetRam(){returnram.toString();}publicStringgetMainboard(){returnmainboard.toString();}publicStringgetHarddisk(){returnharddisk.toString();}publicStringgetDisplay(){returndisplay.toString();}}6(10)抽象产品计算机类的子类PrepareComputer.java:publicclassPrepareComputerextendsComputer{ComputerConfigurationFactoryConfigurationFactory;publicPrepareComputer(ComputerConfigurationFactoryConfigurationFactory){this.ConfigurationFactory=ConfigurationFactory;}publicvoidprepare(){cpu=ConfigurationFactory.createCpu();ram=ConfigurationFactory.createRam();mainboard=ConfigurationFactory.createMainboard();harddisk=ConfigurationFactory.createHarddisk();display=ConfigurationFactory.createDisplay();}}(11)抽象计算机店类ComputerStore.java:publicabstractclassComputerStore{protectedabstractComputercreaterComputer(Stringitem);publicComputerorderComputer(Stringtype){Computercomputer=createrComputer(type);;computer.prepare();returncomputer;}}(12)抽象计算机店类的子类DIYComputerStore.java:publicclassDIYComputerStoreextendsComputerStore{protectedComputercreaterComputer(Stringitem){Computercomputer=null;ComputerConfigurationFactorystudentConfigurationFactory=newStudentComputerConfigurationFactory();ComputerConfigurationFactoryhomeConfigurationFactory=newHomeComputerConfigurationFactory();ComputerConfigurationFactorydeluxeConfigurationFactory=newDeluxeComputerConfigurationFactory();if(item.equals(学生版)){computer=newPrepareComputer(studentConfigurationFactory);computer.setName(学生版);}elseif(item.equals(家庭装))7{computer=newPrepareComputer(homeConfigurationFactory);computer.setName(家庭装);}elseif(item.equals(豪华版)){computer=newPrepareComputer(deluxeConfigurationFactory);computer