生成器模式
[实验任务一]:计算机组装
使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起构成计算机,计算机的类型可以是笔记本,也可以是台式机。
package Tutorial_5; public class Client { public static void main(String[] args) { //动态确定电脑类型 ComputerBuilder cb = (ComputerBuilder) XMLUtil.getBean(); //指挥者 Director director = new Director(); //指挥者准备组装 director.setComputerBuilder(cb); //获得组装 Computer computer = director.construct(); System.out.println("电脑组装"); System.out.println(computer.getCpu()); System.out.println(computer.getMemory()); System.out.println(computer.getHardDisk()); System.out.println(computer.getHost()); } } package Tutorial_5; public class Computer { private String cpu; private String memory; private String hardDisk; private String host; public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getMemory() { return memory; } public void setMemory(String memory) { this.memory = memory; } public String getHardDisk() { return hardDisk; } public void setHardDisk(String hardDisk) { this.hardDisk = hardDisk; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } } package Tutorial_5; public abstract class ComputerBuilder { protected Computer computer=new Computer(); public abstract void buildCpu(); public abstract void buildMemory(); public abstract void buildHardDisk(); public abstract void buildHost(); public Computer getComputer() { return computer; } } package Tutorial_5; public class DesktopBuilder extends ComputerBuilder { @Override public void buildCpu() { computer.setCpu("台式机装了一个Cpu"); } @Override public void buildMemory() { computer.setMemory("台式机装了一个内存"); } @Override public void buildHardDisk() { computer.setHardDisk("台式机装了一个硬盘"); } @Override public void buildHost() { computer.setHost("台式机装了一个主机"); } } package Tutorial_5; public class Director { private ComputerBuilder cb; public void setComputerBuilder(ComputerBuilder cb) { this.cb = cb; } public Computer construct() { cb.buildCpu(); cb.buildMemory(); cb.buildHardDisk(); cb.buildHost(); return cb.getComputer(); } } package Tutorial_5; public class LaptopBuilder extends ComputerBuilder { @Override public void buildCpu() { computer.setCpu("笔记本装了一个Cpu"); } @Override public void buildMemory() { computer.setMemory("笔记本装了一个内存"); } @Override public void buildHardDisk() { computer.setHardDisk("笔记本装了一个硬盘"); } @Override public void buildHost() { computer.setHost("笔记本装了一个主机"); } } package Tutorial_5; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; public class XMLUtil { public static Object getBean() { try { //创建DOM文档对象 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc = builder.parse(new File("src/main/resources/config5.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" encoding="UTF-8"?> <config> <className>Tutorial_5.DesktopBuilder</className> </config>