《设计模式实验4 抽象工厂模式》由会员分享,可在线阅读,更多相关《设计模式实验4 抽象工厂模式(8页珍藏版)》请在金锄头文库上搜索。
1、编号:时间:2021年x月x日书山有路勤为径,学海无涯苦作舟页码:第1页 共1页 实验报告课程: 设计模式实验 学期: 2010-2011学年 第一学期 任课教师: 专业: 学号: 姓名: 成绩: 实验4 抽象工厂模式1.题目: 使用抽象工厂模式实现一个DIY电脑店,其中的电脑配置有三个种类:学生版、家庭装、豪华版。2.模式设计的UML类图:3.程序源代码:(1)计算机配置工厂接口ComputerConfigurationFactory.java:public interface ComputerConfigurationFactorypublic Cpu createCpu();public
2、 Ram createRam();public Mainboard createMainboard();public Harddisk createHarddisk();public Display createDisplay();(2)计算机配置工厂接口的各个子类:public class StudentComputerConfigurationFactory implements ComputerConfigurationFactorypublic Cpu createCpu() return new StudentCpu(); public Ram createRam() return
3、new StudentRam(); public Mainboard createMainboard() return new StudentMainboard(); public Harddisk createHarddisk() return new StudentHarddisk(); public Display createDisplay() return new StudentDisplay(); public class HomeComputerConfigurationFactory implements ComputerConfigurationFactorypublic C
4、pu createCpu() return new HomeCpu(); public Ram createRam() return new HomeRam(); public Mainboard createMainboard() return new HomeMainboard(); public Harddisk createHarddisk() return new HomeHarddisk(); public Display createDisplay() return new HomeDisplay(); public class DeluxeComputerConfigurati
5、onFactory implements ComputerConfigurationFactorypublic Cpu createCpu() return new DeluxeCpu(); public Ram createRam() return new DeluxeRam(); public Mainboard createMainboard() return new DeluxeMainboard(); public Harddisk createHarddisk() return new DeluxeHarddisk(); public Display createDisplay()
6、 return new DeluxeDisplay(); (3)电脑各种配置的接口:public interface Cpu public String toString();public interface Ram public String toString();public interface Mainboard public String toString();public interface Harddisk public String toString();public interface Display public String toString();(4)电脑配置Cpu接口的
7、各个子类:public class StudentCpu implements Cpupublic String toString() return Intel 奔腾双核 E5400(盒); public class HomeCpu implements Cpupublic String toString() return Intel 酷睿i5 650(盒); public class DeluxeCpu implements Cpupublic String toString() return Intel 酷睿i7 980X(至尊版); (5)电脑配置Ram接口的各个子类:public cl
8、ass StudentRam implements Rampublic String toString() return 宇瞻2GB DDR3 1333(经典系列); public class HomeRam implements Rampublic String toString() return 金士顿4GB DDR3 1333(骇客神条套装); public class DeluxeRam implements Rampublic String toString() return 海盗船TW3X4G2000C9DF; (6)电脑配置Mainboard接口的各个子类:public clas
9、s StudentMainboard implements Mainboardpublic String toString() return 铭瑄 MS-M3A890G; public class HomeMainboard implements Mainboardpublic String toString() return 技嘉GA-870A-UD3; public class DeluxeMainboard implements Mainboardpublic String toString() return 华硕Rampage III Extreme; (7)电脑配置Harddisk接
10、口的各个子类:public class StudentHarddisk implements Harddiskpublic String toString() return 希捷250GB 7200.12 8M; public class HomeHarddisk implements Harddiskpublic String toString() return 三星500GB 7200转 16M(串/金宝); public class DeluxeHarddisk implements Harddiskpublic String toString() return 希捷Cheetah 15
11、K.7 600GB; (8)电脑配置Display接口的各个子类:public class StudentDisplay implements Displaypublic String toString() return 飞利浦193E1SB; public class HomeDisplay implements Displaypublic String toString() return 三星P2450H; public class DeluxeDisplay implements Displaypublic String toString() return 戴尔UltraSharp 24
12、08WFP; (9)抽象产品计算机类Computer.java:public abstract class Computer String name;Cpu cpu;Ram ram;Mainboard mainboard;Harddisk harddisk;Display display;public abstract void prepare();public void setName(String name) this.name=name; public String getName() return name; public String getCpu() return cpu.toSt
13、ring(); public String getRam() return ram.toString(); public String getMainboard() return mainboard.toString(); public String getHarddisk() return harddisk.toString(); public String getDisplay() return display.toString(); (10)抽象产品计算机类的子类PrepareComputer.java:public class PrepareComputer extends ComputerComputerConfigurationFactory ConfigurationFactory;public PrepareComputer(ComputerConfigurationFactory ConfigurationFactory)