标签:游戏 sys 相同 就是 客户 new ati board 实例
package designpattern.builder; public class Computer { public String computerCase; public String monitor; public String keyboard; public String mouse; public String getComputerCase() { return computerCase; } public void setComputerCase(String computerCase) { this.computerCase = computerCase; } public String getMonitor() { return monitor; } public void setMonitor(String monitor) { this.monitor = monitor; } public String getKeyboard() { return keyboard; } public void setKeyboard(String keyboard) { this.keyboard = keyboard; } public String getMouse() { return mouse; } public void setMouse(String mouse) { this.mouse = mouse; } @Override public String toString() { return "Computer [computerCase=" + computerCase + ", monitor=" + monitor + ", keyboard=" + keyboard + ", mouse=" + mouse + "]"; } }
package designpattern.builder; public interface ComputerBuilder { void assembleComputerCase(Computer computer);// 组装主机 void assembleMonitor(Computer computer);// 组装显示器 void assembleKeyboard(Computer computer);// 组装键盘 void assembleMouse(Computer computer);// 组装显示器 }
package designpattern.builder; public class GameComputerBuilder implements ComputerBuilder { @Override public void assembleComputerCase(Computer computer) { computer.setComputerCase("水冷机箱"); } @Override public void assembleMonitor(Computer computer) { computer.setMonitor("4k显示器"); } @Override public void assembleKeyboard(Computer computer) { computer.setKeyboard("机械键盘"); } @Override public void assembleMouse(Computer computer) { computer.setMouse("人体工学鼠标"); } }
package designpattern.builder; public class AmusementComputerBuilder implements ComputerBuilder { @Override public void assembleComputerCase(Computer computer) { computer.setComputerCase("普通机箱"); } @Override public void assembleMonitor(Computer computer) { computer.setMonitor("普通显示器"); } @Override public void assembleKeyboard(Computer computer) { computer.setKeyboard("普通键盘"); } @Override public void assembleMouse(Computer computer) { computer.setMouse("普通鼠标"); } }
package designpattern.builder; public class ComputerBuilderDirect { public void assembleComputer(ComputerBuilder computerBuilder, Computer computer) { computerBuilder.assembleComputerCase(computer); computerBuilder.assembleMonitor(computer); computerBuilder.assembleKeyboard(computer); computerBuilder.assembleMouse(computer); } // 或者写成下面这样?可能具体情况具体分析吧 public Computer assembleComputer(ComputerBuilder computerBuilder) { Computer computer = new Computer(); computerBuilder.assembleComputerCase(computer); computerBuilder.assembleMonitor(computer); computerBuilder.assembleKeyboard(computer); computerBuilder.assembleMouse(computer); return computer; } }
package designpattern.builder; public class Client { public static void main(String[] args) { ComputerBuilderDirect computerBuilderDirect = new ComputerBuilderDirect(); // 组装游戏型电脑 Computer gameComputer = new Computer(); computerBuilderDirect.assembleComputer(new GameComputerBuilder(), gameComputer); System.out.println("游戏型电脑:"); System.out.println(gameComputer); // 组装普通娱乐型电脑 Computer assembleComputer = new Computer(); computerBuilderDirect.assembleComputer(new AmusementComputerBuilder(), assembleComputer); System.out.println("普通娱乐型电脑:"); System.out.println(assembleComputer); } }
游戏型电脑: Computer [computerCase=水冷机箱, monitor=4k显示器, keyboard=机械键盘, mouse=人体工学鼠标] 普通娱乐型电脑: Computer [computerCase=普通机箱, monitor=普通显示器, keyboard=普通键盘, mouse=普通鼠标]
标签:游戏 sys 相同 就是 客户 new ati board 实例
原文地址:https://www.cnblogs.com/imoqian/p/10727296.html