1、肖正文抽象工厂模式云南大学软件学院实 验 报 告序号: 姓名: 肖正文 学号: 20121120141 专业:软件工程 日期:2014/12/18成绩: 实验三 抽象工厂模式的运用一、实验目的: 抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。在熟悉抽象工厂模式相关理论知识的基础上,使用抽象工厂模式实现“电脑配置商店”程序。二、实验要求:使用抽象工厂模式实现“电脑配置商店”,要求如下:1. 电脑由CPU、主板、显卡、内存、硬盘等配件组合而成。现电脑配置有两种配置方案。2. 根据配置方案可以显示具体配置信息。3. 根据配置方案可以显示价格。1、 设计并绘制该程序的
2、类图;2、 依照设计的类图使用Java语言编写代码,并实现该程序;3、 除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:a) 控制台程序,Client硬编码初始化模式和测试环境,运行结果文本输出;b) 控制台程序,Client初始化测试环境,并根据用户输入运算,运行结果文本输出;c) 设计并实现用户UI,Client初始化测试环境,并根据用户在UI控件上的输入运算,运行结果文本输出;三、实验内容:1类图2被装饰类package xiao.it.abGoods;/* * 抽象的产品类 */import xiao.it.ComIngredient.*;public abstract
3、 class Computer protected String name = null; protected CPU cpu = null; protected HardDisk hardd = null; protected MasterBord mb = null; protected NVIDIA nvidia = null; protected RAM ram = null; / 抽象的产品准备方法 public abstract void Prepare(); public String getName() return name; public void setName(Stri
4、ng name) this.name = name; /* * 显示电脑的配置信息 */ public String showInfo() return this.name +n+ this.cpu.getDesc()+n + this.hardd.getDesc() +n+ this.mb.getDesc() +n+ this.nvidia.getDesc()+n + this.ram.getDesc(); /* * 显示电脑的花费价钱 */ public double showCost() return this.cpu.getCost()+this.hardd.getCost()+thi
5、s.mb.getCost()+this.nvidia.getCost()+this.ram.getCost(); package xiao.it.abFactor;/* * 抽象工厂 */import xiao.it.ComIngredient.*; public interface ComputerIngredientFactory public CPU createCUP(); public HardDisk createHardD(); public MasterBord createMasterB(); public NVIDIA createNV(); public RAM crea
6、teR();具体工厂package xiao.it.conFactor;/* * Dura原料工场 */import xiao.it.ComIngredient.CPU;import xiao.it.ComIngredient.HardDisk;import xiao.it.ComIngredient.MasterBord;import xiao.it.ComIngredient.NVIDIA;import xiao.it.ComIngredient.RAM;import xiao.it.abFactor.ComputerIngredientFactory; public class Dura
7、Factor implements ComputerIngredientFactory Override public CPU createCUP() return new CPU(duraCUP 2.5HZ,1200); Override public HardDisk createHardD() / TODO Auto-generated method stub return new HardDisk(duraDisk 750G,400); Override public MasterBord createMasterB() / TODO Auto-generated method stu
8、b return new MasterBord(duraMb,1600); Override public NVIDIA createNV() / TODO Auto-generated method stub return new NVIDIA(duraNVIDIA 610*2MB, 600); Override public RAM createR() / TODO Auto-generated method stub return new RAM(duraRAM 2G, 300); package xiao.it.conFactor;import xiao.it.ComIngredien
9、t.CPU;import xiao.it.ComIngredient.HardDisk;import xiao.it.ComIngredient.MasterBord;import xiao.it.ComIngredient.NVIDIA;import xiao.it.ComIngredient.RAM;import xiao.it.abFactor.ComputerIngredientFactory;/* * intel原料工场 * author Administrator * */public class IntelFactor implements ComputerIngredientF
10、actory Override public CPU createCUP() CPU c=new CPU(intelduraCUP 3.1HZ, 1600); return c; Override public HardDisk createHardD() / TODO Auto-generated method stub return new HardDisk(IntelDisk 1T,400); Override public MasterBord createMasterB() / TODO Auto-generated method stub return new MasterBord
11、(IntelduraMb,1600); Override public NVIDIA createNV() / TODO Auto-generated method stub return new NVIDIA(intelNVIDIA 500*2MB, 600); Override public RAM createR() / TODO Auto-generated method stub return new RAM(intelRAM 4G, 500); 具体被装饰者package xiao.it.conGoods;import xiao.it.abFactor.ComputerIngred
12、ientFactory;import xiao.it.abGoods.Computer;public class HongJi extends Computer ComputerIngredientFactory com=null; /为宏基电脑分配工厂 public HongJi(ComputerIngredientFactory com) =com; /用特定的工厂来为宏基配置 Override public void Prepare() cpu=com.createCUP(); hardd=com.createHardD(); mb=com.createMasterB(); nvidia
13、=com.createNV(); ram=com.createR(); package xiao.it.conGoods;/* * 华硕电脑 */import xiao.it.abFactor.ComputerIngredientFactory;import xiao.it.abGoods.Computer;public class HuaShuo extends Computer ComputerIngredientFactory com=null; /为华硕电脑分配工厂 public HuaShuo(ComputerIngredientFactory com) =com; /用特定的工厂来
14、为宏基配置 Override public void Prepare() cpu=com.createCUP(); hardd=com.createHardD(); mb=com.createMasterB(); nvidia=com.createNV(); ram=com.createR(); package xiao.it.conGoods;import xiao.it.abFactor.ComputerIngredientFactory;import xiao.it.abGoods.Computer;public class LianXiang extends Computer Comp
15、uterIngredientFactory com=null; /为联想电脑分配工厂 public LianXiang(ComputerIngredientFactory com) =com; /用特定的工厂来为宏基配置 Override public void Prepare() cpu=com.createCUP(); hardd=com.createHardD(); mb=com.createMasterB(); nvidia=com.createNV(); ram=com.createR(); 原料类package xiao.it.ComIngredient;/* * cpu * au
16、thor Administrator * */public class CPU private String desc; private double cost; public String getDesc() return desc; public void setDesc(String desc) this.desc = desc; public double getCost() return cost; public void setCost(double cost) this.cost = cost; public CPU(String desc, double cost) super
17、(); this.desc = desc; this.cost = cost; package xiao.it.ComIngredient;/* * 硬盘 * author Administrator * */public class HardDisk private String desc; private double cost; public String getDesc() return desc; public void setDesc(String desc) this.desc = desc; public double getCost() return cost; public
18、 void setCost(double cost) this.cost = cost; public HardDisk(String desc, double cost) super(); this.desc = desc; this.cost = cost; package xiao.it.ComIngredient;/* * 主板 * author Administrator * */public class MasterBord private String desc; private double cost; public String getDesc() return desc;
19、public void setDesc(String desc) this.desc = desc; public double getCost() return cost; public void setCost(double cost) this.cost = cost; public MasterBord(String desc, double cost) super(); this.desc = desc; this.cost = cost; package xiao.it.ComIngredient;/* * 显卡 * author Administrator * */public
20、class NVIDIA private String desc; private double cost; public String getDesc() return desc; public void setDesc(String desc) this.desc = desc; public double getCost() return cost; public void setCost(double cost) this.cost = cost; public NVIDIA(String desc, double cost) super(); this.desc = desc; th
21、is.cost = cost; package xiao.it.ComIngredient;public class RAM private String desc; private double cost; public String getDesc() return desc; public void setDesc(String desc) this.desc = desc; public double getCost() return cost; public void setCost(double cost) this.cost = cost; public RAM(String d
22、esc, double cost) super(); this.desc = desc; this.cost = cost; 商店类package xiao.it.abGoodsStore;/* * 电脑商城 */import xiao.it.abGoods.Computer;public abstract class ComputerStor public Computer orderC(String type) Computer c=null; c=CreateComputer(type); return c; /抽象方法去创建电脑交给子类 protected abstract Compu
23、ter CreateComputer(String type);package xiao.it.conGoodsStore;/* * 贵州商城 */import xiao.it.abFactor.ComputerIngredientFactory;import xiao.it.abGoods.Computer;import xiao.it.abGoodsStore.ComputerStor;import xiao.it.conFactor.IntelFactor;import xiao.it.conGoods.HongJi;import xiao.it.conGoods.HuaShuo;imp
24、ort xiao.it.conGoods.LianXiang;public class GZStore extends ComputerStor ComputerIngredientFactory factory = null; /为商店配置一个配置方案 public GZStore(ComputerIngredientFactory fa) this.factory=fa; Override protected Computer CreateComputer(String type) Computer c = null; / intel的配置方案 / TODO Auto-generated
25、method stub if (type.equals(宏基) c = new HongJi(factory); c.setName(宏基电脑); else if (type.equals(华硕) c = new HuaShuo(factory); c.setName(华硕电脑); else if (type.equals(联想) c = new LianXiang(factory); c.setName(联想电脑); return c; package xiao.it.conGoodsStore;/* * 四川商城 */import xiao.it.abFactor.ComputerIngr
26、edientFactory;import xiao.it.abGoods.Computer;import xiao.it.abGoodsStore.ComputerStor;import xiao.it.conFactor.IntelFactor;import xiao.it.conGoods.HongJi;import xiao.it.conGoods.HuaShuo;import xiao.it.conGoods.LianXiang;public class SCStore extends ComputerStor ComputerIngredientFactory factory = n
27、ull; /为商店配置一个配置方案 public SCStore(ComputerIngredientFactory fa) this.factory=fa; Override protected Computer CreateComputer(String type) Computer c = null; / TODO Auto-generated method stub if (type.equals(宏基) c = new HongJi(factory); c.setName(宏基电脑); else if (type.equals(华硕) c = new HuaShuo(factory)
28、; c.setName(华硕电脑); else if (type.equals(联想) c = new LianXiang(factory); c.setName(联想电脑); return c; package xiao.it.conGoodsStore;/* * 云南商城 */import xiao.it.abFactor.ComputerIngredientFactory;import xiao.it.abGoods.Computer;import xiao.it.abGoodsStore.ComputerStor;import xiao.it.conFactor.IntelFactor;import xiao.it.conGoods.*;public class YNStore extends ComputerStor ComputerIngredientFactory factory = null; /为商店配置一个配置方案