10.29
席發表於2024-10-30
實驗5:建造者模式本次實驗屬於模仿型實驗,透過本次實驗學生將掌握以下內容: 1、理解建造者模式的動機,掌握該模式的結構;2、能夠利用建造者模式解決實際問題。 [實驗任務一]:計算機組裝
使用建造者模式,完成下述任務:計算機組裝工廠可以將CPU、記憶體、硬碟、主機等硬體裝置組裝在一起構成計算機,計算機的型別可以是筆記本,也可以是桌上型電腦。
實驗要求:
1. 畫出對應的類圖;
2. 提交原始碼;
import BuilderPattern.complexProduct.Computer;
//建造者類
public abstract class ComputerBuilder {
protected Computer computer = new Computer();
public abstract void buildCPUs();//CPU
public abstract void buildmonitor();//顯示器
public abstract void buildhardwareEquipment();//筆記本使用自己獨自的一套硬體裝置
public Computer getComputer(){
return computer;
}
}
//複合產品類
public class Computer {
private String CPUs;//CPU
private String monitor;//顯示器
private String hardwareEquipment;//筆記本使用自己獨自的一套硬體裝置
public String getCPUs() {
return CPUs;
}
public void setCPUs(String CPUs) {
this.CPUs = CPUs;
}
public String getMonitor() {
return monitor;
}
public void setMonitor(String monitor) {
this.monitor = monitor;
}
public String getHardwareEquipment() {
return hardwareEquipment;
}
public void setHardwareEquipment(String hardwareEquipment) {
this.hardwareEquipment = hardwareEquipment;
}
}
import BuilderPattern.abstractBuilder.ComputerBuilder;
public class Desktop extends ComputerBuilder {
public void buildCPUs(){
computer.setCPUs("CPU 記憶體 硬碟 主機 ");
}
public void buildmonitor(){
computer.setMonitor("顯示器 ");
}
public void buildhardwareEquipment(){
computer.setHardwareEquipment("null");
}
}
import BuilderPattern.abstractBuilder.ComputerBuilder;
public class NoteBook extends ComputerBuilder {
public void buildCPUs(){
computer.setCPUs("null ");
}
public void buildmonitor(){
computer.setMonitor("null ");
}
public void buildhardwareEquipment(){
computer.setHardwareEquipment("筆記本使用自己獨自的一套硬體裝置");
}
}
import BuilderPattern.abstractBuilder.ComputerBuilder;
public class Server extends ComputerBuilder {
public void buildCPUs(){
computer.setCPUs("CPU 記憶體 硬碟 主機 ");
}
public void buildmonitor(){
computer.setMonitor("null ");
}
public void buildhardwareEquipment(){
computer.setHardwareEquipment("null");
}
}
import BuilderPattern.abstractBuilder.ComputerBuilder;
import BuilderPattern.complexProduct.Computer;
public class ComputerAssembleDirector {
private ComputerBuilder cb;
public void setCb(ComputerBuilder cb) {
this.cb = cb;
}
public Computer constract(){
cb.buildCPUs();
cb.buildmonitor();
cb.buildhardwareEquipment();
return cb.getComputer();
}
}
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
import java.io.*;
public class XMLUtil {
public static Object getBean(){
try {
//建立文件物件
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("src/BuilderPattern/Utils/config.xml"));
//獲取包含支付方式名稱的文字節點
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
//透過類名生成例項物件並將其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
<?xml version="1.0" ?>
<config>
<className>BuilderPattern.complexBuilder.Desktop</className>
</config>
import BuilderPattern.Utils.XMLUtil;
import BuilderPattern.abstractBuilder.ComputerBuilder;
import BuilderPattern.complexProduct.Computer;
import BuilderPattern.conductor.ComputerAssembleDirector;
import abstractFactory.abstractFactory.AbstractFactory;
public class Client {
public static void main(String[] args) {
ComputerBuilder cb = (ComputerBuilder) XMLUtil.getBean();
ComputerAssembleDirector cad = new ComputerAssembleDirector();
cad.setCb(cb);
Computer computer = cad.constract();
System.out.println("計算機的組成:");
System.out.println(computer.getCPUs());
System.out.println(computer.getMonitor());
System.out.println(computer.getHardwareEquipment());
}
}
3.注意程式設計規範。