package ruanjiansheji.shiyan5; public class Computer { private String CPU; private String RAM; private String Hard; private String Host; public String getCPU() { return CPU; } public void setCPU(String CPU) { this.CPU = CPU; } public String getRAM() { return RAM; } public void setRAM(String RAM) { this.RAM = RAM; } public String getHard() { return Hard; } public void setHard(String hard) { Hard = hard; } public String getHost() { return Host; } public void setHost(String host) { Host = host; } @Override public String toString() { return "Computer{" + "CPU='" + CPU + '\'' + ", RAM='" + RAM + '\'' + ", Hard='" + Hard + '\'' + ", Host='" + Host + '\'' + '}'; } } package ruanjiansheji.shiyan5; public abstract class ComputerBuilder { protected Computer computer=new Computer(); public abstract void buildCPU(); public abstract void buildRAM(); public abstract void buildHard(); public abstract void buildHost(); public Computer getComputer() { return computer; } } package ruanjiansheji.shiyan5; public class DesktopComputer extends ComputerBuilder{ public void buildCPU() { computer.setCPU("台式机CPU"); } public void buildRAM() { computer.setRAM("台式机内存"); } public void buildHard() { computer.setHard("台式机硬盘"); } public void buildHost() { computer.setHost("台式机主机"); } } package ruanjiansheji.shiyan5; public class Director { private ComputerBuilder computerBuilder; public void setComputerBuilder(ComputerBuilder c){ computerBuilder=c; } public Computer construct(){ computerBuilder.buildCPU(); computerBuilder.buildRAM(); computerBuilder.buildHard(); computerBuilder.buildHost(); return computerBuilder.getComputer(); } } package ruanjiansheji.shiyan5; public class LaptopComputer extends ComputerBuilder{ public void buildCPU() { computer.setCPU("笔记本CPU"); } public void buildRAM() { computer.setRAM("笔记本内存"); } public void buildHard() { computer.setHard("笔记本硬盘"); } public void buildHost() { computer.setHost("笔记本主机"); } } package ruanjiansheji.shiyan5; public class main { public static void main(String[] args) { Director director=new Director(); ComputerBuilder cb; System.out.println("安装笔记本"); cb=new LaptopComputer(); director.setComputerBuilder(cb); Computer Laptop = director.construct(); System.out.println(Laptop.toString()); System.out.println("安装台式机"); cb=new DesktopComputer(); director.setComputerBuilder(cb); Computer Desktop = director.construct(); System.out.println(Desktop.toString()); } }
标签:String,Computer,10.28,void,public,computer,CPU From: https://www.cnblogs.com/galileo9527/p/18509578