建造者模式
建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。
这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
解决问题:
当一个类的构造函数超过4个,而且某些参数是可选的,我们通常有两个方法来构建他的对象。
public class Computer {
private String cpu;//必须
private String ram;//必须
private int usbCount;//可选
private String keyboard;//可选
private String display;//可选
}
第一:折叠构造函数模式
public class Computer {
...
public Computer(String cpu, String ram) {
this(cpu, ram, 0);
}
public Computer(String cpu, String ram, int usbCount) {
this(cpu, ram, usbCount, "罗技键盘");
}
public Computer(String cpu, String ram, int usbCount,
String keyboard) {
this(cpu, ram, usbCount, keyboard, "三星显示器");
}
public Computer(String cpu, String ram, int usbCount,
String keyboard, String display) {
this.cpu = cpu;
this.ram = ram;
this.usbCount = usbCount;
this.keyboard = keyboard;
this.display = display;
}
}
第二种:Javabean 模式
public class Computer {
...
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 int getUsbCount() {
return usbCount;
}
...
}
弊端:
第一种主要是使用及阅读不方便。
第二种方式在构建过程中对象的状态容易发生变化,造成错误。
解决问题
适用建造者模式
- 目标类以及静态建造类
package org.kouhao.design.patterns.建造者模式;
/**
* 目标类:电脑
*
* @author admin
*/
public class Computer {
private String cpu;//必须
private String ram;//必须
private int usbCount;//可选
private String keyboard;//可选
private String display;//可选
public Computer(String cpu, String ram) {
this.cpu = cpu;
this.ram = ram;
}
public Computer(ComputerBuilder computerBuilder) {
this.cpu = computerBuilder.cpu;
this.ram = computerBuilder.ram;
this.usbCount = computerBuilder.usbCount;
this.keyboard = computerBuilder.keyboard;
this.display = computerBuilder.display;
}
public void setUsbCount(int usbCount) {
this.usbCount = usbCount;
}
public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}
public void setDisplay(String display) {
this.display = display;
}
@Override
public String toString() {
return "Computer{" +
"cpu='" + cpu + '\'' +
", ram='" + ram + '\'' +
", usbCount=" + usbCount +
", keyboard='" + keyboard + '\'' +
", display='" + display + '\'' +
'}';
}
public static class ComputerBuilder {
private String cpu;//必须
private String ram;//必须
private int usbCount;//可选
private String keyboard;//可选
private String display;//可选
public ComputerBuilder(String cpu, String ram) {
this.cpu = cpu;
this.ram = ram;
}
public ComputerBuilder usbCount(int usbCount) {
this.usbCount = usbCount;
return this;
}
public ComputerBuilder keyboard(String keyboard) {
this.keyboard = keyboard;
return this;
}
public ComputerBuilder display(String display) {
this.display = display;
return this;
}
public Computer build() {
return new Computer(this);
}
}
}
- 客户端
package org.kouhao.design.patterns.建造者模式;
/**
* @author admin
*/
public class Client {
public static void main(String[] args) {
Computer.ComputerBuilder computerBuilder = new Computer.ComputerBuilder("Inter", "").
usbCount(1).display("display").keyboard("keyboard");
computerBuilder.build();
}
}
标签:String,ram,建造,模式,public,keyboard,usbCount,cpu
From: https://www.cnblogs.com/kouhao/p/17503107.html