Java建造者模式是一种创建型设计模式,通过一步一步的构建复杂对象来实现构建者模式。
下面是Java建造者模式构建复杂对象的完整攻略:
步骤一:创建一个产品类
创建一个产品类,该类由多个属性组成,并提供setter和getter方法。
public class Computer {
private String cpu;
private String memory;
private String disk;
private String graphicsCard;
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 getDisk() {
return disk;
}
public void setDisk(String disk) {
this.disk = disk;
}
public String getGraphicsCard() {
return graphicsCard;
}
public void setGraphicsCard(String graphicsCard) {
this.graphicsCard = graphicsCard;
}
}
步骤二:创建一个抽象建造者类
创建一个抽象建造者类,该类定义了一个用于建造产品的接口及其需要的方法。
public abstract class ComputerBuilder {
public abstract void buildCpu();
public abstract void buildMemory();
public abstract void buildDisk();
public abstract void buildGraphicsCard();
public abstract Computer getComputer();
}
步骤三:创建具体建造者类
创建一个具体的建造者类,实现抽象建造者接口中的方法。
public class LenovoComputerBuilder extends ComputerBuilder {
private Computer computer = new Computer();
public void buildCpu() {
computer.setCpu("Intel Core i7");
}
public void buildMemory() {
computer.setMemory("16G DDR4");
}
public void buildDisk() {
computer.setDisk("1TB SSD");
}
public void buildGraphicsCard() {
computer.setGraphicsCard("NVIDIA RTX 2060");
}
public Computer getComputer() {
return computer;
}
}
步骤四:创建一个指导者类
创建一个指导者类,用于使用建造者接口中的方法来建造产品对象。
public class Director {
private ComputerBuilder computerBuilder;
public void setComputerBuilder(ComputerBuilder computerBuilder) {
this.computerBuilder = computerBuilder;
}
public void constructComputer() {
computerBuilder.buildCpu();
computerBuilder.buildMemory();
computerBuilder.buildDisk();
computerBuilder.buildGraphicsCard();
}
}
步骤五:使用建造者模式构建复杂对象
使用建造者模式构建复杂对象。
public class Client {
public static void main(String[] args) {
Director director = new Director();
ComputerBuilder builder = new LenovoComputerBuilder();
director.setComputerBuilder(builder);
director.constructComputer();
Computer computer = builder.getComputer();
System.out.println("CPU: " + computer.getCpu());
System.out.println("Memory: " + computer.getMemory());
System.out.println("Disk: " + computer.getDisk());
System.out.println("Graphics card: " + computer.getGraphicsCard());
}
}
以上就是使用Java建造者模式构建复杂对象的完整攻略。下面是两个示例说明。
示例1
如果我们想要另一台具有不同特征的电脑,我们可以使用另一个具体构建者类。
public class HpComputerBuilder extends ComputerBuilder {
private Computer computer = new Computer();
public void buildCpu() {
computer.setCpu("AMD Ryzen 7");
}
public void buildMemory() {
computer.setMemory("32G DDR4");
}
public void buildDisk() {
computer.setDisk("2TB SSD");
}
public void buildGraphicsCard() {
computer.setGraphicsCard("NVIDIA RTX 3080");
}
public Computer getComputer() {
return computer;
}
}
在客户端代码处,只需要更改建造者对象为HpComputerBuilder即可。
public class Client {
public static void main(String[] args) {
Director director = new Director();
ComputerBuilder builder = new HpComputerBuilder();
director.setComputerBuilder(builder);
director.constructComputer();
Computer computer = builder.getComputer();
System.out.println("CPU: " + computer.getCpu());
System.out.println("Memory: " + computer.getMemory());
System.out.println("Disk: " + computer.getDisk());
System.out.println("Graphics card: " + computer.getGraphicsCard());
}
}
示例2
我们可以调用具体建造者中的任何方法来构建电脑对象的组件。例如,我们可以在不修改Computer类的情况下添加其他组件。
public class LenovoComputerBuilder extends ComputerBuilder {
private Computer computer = new Computer();
public void buildCpu() {
computer.setCpu("Intel Core i7");
}
public void buildMemory() {
computer.setMemory("16G DDR4");
}
public void buildDisk() {
computer.setDisk("1TB SSD");
}
public void buildGraphicsCard() {
computer.setGraphicsCard("NVIDIA RTX 2060");
}
public void buildExtraComponent() {
computer.setExtraComponent("Wireless mouse and keyboard set");
}
public Computer getComputer() {
return computer;
}
}
在客户端处,我们可以轻松地使用新方法。
public class Client {
public static void main(String[] args) {
Director director = new Director();
ComputerBuilder builder = new LenovoComputerBuilder();
director.setComputerBuilder(builder);
director.constructComputer();
LenovoComputerBuilder lenovoComputerBuilder = (LenovoComputerBuilder) builder;
lenovoComputerBuilder.buildExtraComponent();
Computer computer = builder.getComputer();
System.out.println("CPU: " + computer.getCpu());
System.out.println("Memory: " + computer.getMemory());
System.out.println("Disk: " + computer.getDisk());
System.out.println("Graphics card: " + computer.getGraphicsCard());
System.out.println("Extra component: " + computer.getExtraComponent());
}
}
以上就是两个示例说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java建造者模式构建复杂对象的最佳实践 - Python技术站