设计模式实验4 抽象工厂模式.doc

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验报告
课程:设计模式实验学期:2010-2011学年第一学期任课教师:
专业:学号:姓名:成绩:
实验4 抽象工厂模式
1.题目:
使用抽象工厂模式实现一个DIY电脑店,其中的电脑配置有三个种类:学生版、家庭装、豪华版。

2.模式设计的UML类图:
3.程序源代码:
(1)计算机配置工厂接口ComputerConfigurationFactory.java:public interface ComputerConfigurationFactory
{
public Cpu createCpu();
public Ram createRam();
public Mainboard createMainboard();
public Harddisk createHarddisk();
public Display createDisplay();
}
(2)计算机配置工厂接口的各个子类:
public class StudentComputerConfigurationFactory implements ComputerConfigurationFactory
{
public Cpu createCpu()
{ return new StudentCpu(); }
public Ram createRam()
{ return new StudentRam(); }
public Mainboard createMainboard()
{ return new StudentMainboard(); }
public Harddisk createHarddisk()
{ return new StudentHarddisk(); }
public Display createDisplay()
{ return new StudentDisplay(); }
}
public class HomeComputerConfigurationFactory implements ComputerConfigurationFactory
{
public Cpu 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 DeluxeComputerConfigurationFactory implements ComputerConfigurationFactory
{
public 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()
{ 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接口的各个子类:
public class StudentCpu implements Cpu {
public String toString()
{ return"Intel 奔腾双核 E5400(盒)"; } }
public class HomeCpu implements Cpu
{
public String toString()
{ return"Intel 酷睿i5 650(盒)"; } }
public class DeluxeCpu implements Cpu
{
public String toString()
{ return"Intel 酷睿i7 980X(至尊版)"; }
}
(5)电脑配置Ram接口的各个子类:
public class StudentRam implements Ram
{
public String toString()
{ return"宇瞻2GB DDR3 1333(经典系列)"; }
}
public class HomeRam implements Ram
{
public String toString()
{ return"金士顿4GB DDR3 1333(骇客神条套装)"; } }
public class DeluxeRam implements Ram
{
public String toString()
{ return"海盗船TW3X4G2000C9DF"; }
}
(6)电脑配置Mainboard接口的各个子类:
public class StudentMainboard implements Mainboard {
public String toString()
{ return"铭瑄 MS-M3A890G"; }
}
public class HomeMainboard implements Mainboard {
public String toString()
{ return"技嘉GA-870A-UD3"; }
}
public class DeluxeMainboard implements Mainboard {
public String toString()
{ return"华硕Rampage III Extreme"; }
}
(7)电脑配置Harddisk接口的各个子类:
public class StudentHarddisk implements Harddisk {
public String toString()
{ return"希捷250GB 7200.12 8M"; }
}
public class HomeHarddisk implements Harddisk
{
public String toString()
{ return"三星500GB 7200转 16M(串/金宝)"; }
}
public class DeluxeHarddisk implements Harddisk {
public String toString()
{ return"希捷Cheetah 15K.7 600GB"; }
}
(8)电脑配置Display接口的各个子类:
public class StudentDisplay implements Display {
public String toString()
{ return"飞利浦193E1SB"; }
}
public class HomeDisplay implements Display
{
public String toString()
{ return"三星P2450H"; }
}
public class DeluxeDisplay implements Display {
public String toString()
{ return"戴尔UltraSharp 2408WFP"; }
}
(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)
{ =name; }
public String getName()
{ return name; }
public String getCpu()
{ return cpu.toString(); }
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 Computer
{
ComputerConfigurationFactory ConfigurationFactory;
public PrepareComputer(ComputerConfigurationFactory
ConfigurationFactory) {
this.ConfigurationFactory = ConfigurationFactory;
}
public void prepare()
{
cpu = ConfigurationFactory.createCpu();
ram = ConfigurationFactory.createRam();
mainboard = ConfigurationFactory.createMainboard();
harddisk = ConfigurationFactory.createHarddisk();
display = ConfigurationFactory.createDisplay();
}
}
(11)抽象计算机店类ComputerStore.java:
public abstract class ComputerStore
{
protected abstract Computer createrComputer(String item);
public Computer orderComputer(String type)
{
Computer computer = createrComputer(type);;
computer.prepare();
return computer;
}
}
(12)抽象计算机店类的子类DIYComputerStore.java:
public class DIYComputerStore extends ComputerStore
{
protected Computer createrComputer(String item)
{
Computer computer = null;
ComputerConfigurationFactory studentConfigurationFactory =
new StudentComputerConfigurationFactory();
ComputerConfigurationFactory homeConfigurationFactory =
new HomeComputerConfigurationFactory();
ComputerConfigurationFactory deluxeConfigurationFactory =
new DeluxeComputerConfigurationFactory();
if(item.equals("学生版"))
{
computer = new
PrepareComputer(studentConfigurationFactory);
computer.setName("学生版");
}
else if(item.equals("家庭装"))
{
computer = new PrepareComputer(homeConfigurationFactory);
computer.setName("家庭装");
}
else if(item.equals("豪华版"))
{
computer = new
PrepareComputer(deluxeConfigurationFactory);
computer.setName("豪华版");
}
return computer;
}
}
(13)实现用户界面的主类DIYStore.java:(由于代码过长,只列出主要部分)
public class DIYStore extends javax.swing.JFrame {
public DIYStore() {
super("XXXXX");
initComponents();
}
private void initComponents()
{ jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("种类"));
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText("学生版");
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("家庭装");
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText("豪华版");
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("配置"));
jLabel1.setText("CPU:");jLabel2.setText("内存:");
jLabel3.setText("主板:");jLabel4.setText("硬盘:");
jLabel5.setText("显示器:");jTextField1.setText("");
jTextField2.setText("");jTextField3.setText("");
jTextField4.setText(""); jTextField5.setText("");
jButton1.setText("退出");jButton2.setText("清空");
jButton3.setText("确定");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{ System.exit(0);}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { buttonGroup1.clearSelection();
jTextField1.setText("");jTextField2.setText("");
jTextField3.setText("");jTextField4.setText("");
jTextField5.setText("");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String[] configuration = getConfiguration();
jTextField1.setText(configuration[0]);
jTextField2.setText(configuration[1]);
jTextField3.setText(configuration[2]);
jTextField4.setText(configuration[3]);
jTextField5.setText(configuration[4]);
}
public String[] getConfiguration()
{
ComputerStore DIYstore = new DIYComputerStore();
Computer computer = null;
String[] configuration = new String[5];
if(jRadioButton1.isSelected())
computer = DIYstore.orderComputer("学生版");
else if(jRadioButton2.isSelected())
computer = DIYstore.orderComputer("家庭装");
else if(jRadioButton3.isSelected())
computer = DIYstore.orderComputer("豪华版");
configuration[0] = computer.getCpu();
configuration[1] = computer.getRam();
configuration[2] = computer.getMainboard();
configuration[3] = computer.getHarddisk();
configuration[4] = computer.getDisplay();
return configuration;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new DIYStore().setVisible(true);
}
});
}}。

相关文档
最新文档