在实际开发过程中,我们可能遇到过这么一个问题:创建一个比较复杂的对象的时候,它有特别多的属性,比如需要创建一个电脑对象,我们就需要设置例如主板、显卡、内存、显示器、鼠标、键盘等等一系列的值。可能比较常规的做法如下:
public class Computer {
private String graphics;
private String memory;
private String cpu;
private String os;
public Computer(String graphics,String memory,String cpu,String os) {
this.graphics = graphics;
this.memory = memory;
this.cpu = cpu;
this.os = os;
}
public static void main(String[] args) {
Computer c = new Computer("NVIDA 980","8G","I7","WINDOWS");
}
}
看起来挺正常的。
但实际上我们要组建一台电脑肯定不是这么简单的,可能我们要先有一个主板,再按照顺序将内存、显卡、cpu、风扇等一些部件添加上去,然后是外设,最后还需要操作系统。
如何我们需要创建一个电脑对象,通过在构造方法里面传递参数肯定是不行的,既然这样干脆加上setter方法吧。
public class Computer {
private String board;
private String cpu;
private String memory;
private String fan;
private String os;
public void setBoard(String board) {
this.board = board;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public void setMemory(String memory) {
this.memory = memory;
}
public void setFan(String fan) {
this.fan = fan;
}
public void setOs(String os) {
this.os = os;
}
}
但是这样又有一个问题,我们创建电脑的时候需要指定一个顺序,这又如何做呢?
所以使用builder模式能够解决我们的问题。
Builder模式的类图
在builder模式中主要有4种角色:
- Product类:产品抽象类。
- Builder:抽象的Builder类,定义了产品的组建行为。
- ConcreteBuilder:具体的builder实现类
- Director:组建产品的过程
当我们需要创建的产品由多个部件组成的时候,不同的组建顺序可能导致创建的产品不同,这时候使用builder模式是非常合适。
我们还是以创建电脑对象来说明builder模式是怎么使用的。
抽象产品类:
public abstract class Computer {
private String board;
private String cpu;
private String memory;
private String fan;
private String os;
public void setBoard(String board) {
this.board = board;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public void setMemory(String memory) {
this.memory = memory;
}
public void setFan(String fan) {
this.fan = fan;
}
public void setOs(String os) {
this.os = os;
}
public abstract void open();
}
产品类:
public class MacComputer extends Computer {
@Override
public void open() {
System.out.println("open IOS system");
}
}
public class LenovoComputer extends Computer {
@Override
public void open() {
System.out.println("open windows system");
}
}
抽象build类:
public abstract class Builder {
public abstract void buildBoard(String board);
public abstract void buildMemory(String memory);
public abstract void buildCPU(String cpu);
public abstract void buildFan(String fan);
public abstract void buildOs();
public abstract Computer create();
}
具体build类:
public class MacBuilder extends Builder {
Computer computer = new LenovoComputer();
@Override
public void buildBoard(String board) {
computer.setBoard(board);
}
@Override
public void buildMemory(String memory) {
computer.setMemory(memory);
}
@Override
public void buildCPU(String cpu) {
computer.setCpu(cpu);
}
@Override
public void buildFan(String fan) {
computer.setFan(fan);
}
@Override
public void buildOs() {
computer.setOs("IOS");
}
@Override
public Computer create() {
return computer;
}
}
public class LenovoBuilder extends Builder {
Computer computer = new LenovoComputer();
@Override
public void buildBoard(String board) {
computer.setBoard(board);
}
@Override
public void buildMemory(String memory) {
computer.setMemory(memory);
}
@Override
public void buildCPU(String cpu) {
computer.setCpu(cpu);
}
@Override
public void buildFan(String fan) {
computer.setFan(fan);
}
@Override
public void buildOs() {
computer.setOs("Windows");
}
@Override
public Computer create() {
return computer;
}
}
组建类:
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct(String board,String cpu,String memory,String fan) {
builder.buildBoard(board);
builder.buildCPU(cpu);
builder.buildMemory(memory);
builder.buildFan(fan);
builder.buildOs();
}
}
测试类:
public class Test {
public static void main(String[] args) {
Builder lenovoBuilder = new LenovoBuilder();
Director lenovoDirector = new Director(lenovoBuilder);
lenovoDirector.construct("主板1","I7","8G","美的");
Computer lenovoComputer = lenovoBuilder.create();
Builder macBuilder = new MacBuilder();
Director macDirector = new Director(macBuilder);
macDirector.construct("主板2","I7","5G","奥克斯");
Computer macComputer = macBuilder.create();
lenovoComputer.open();
macComputer.open();
}
}
在实际开发中,builder模式也出现了一种变种写法,builder同时容纳了build类本身的功能和Director功能。我们将上面的代码修改一下:
public class LenovoComputer extends Computer {
@Override
public void open() {
System.out.println("open windows system");
}
public static class Builder{
private String board;
private String cpu;
private String memory;
private String fan;
private String os;
public Builder setBoard(String board) {
this.board = board;
return this;
}
public Builder setCpu(String cpu) {
this.cpu = cpu;
return this;
}
public Builder setMemory(String memory) {
this.memory = memory;
return this;
}
public Builder setFan(String fan) {
this.fan = fan;
return this;
}
public Builder setOs(String os) {
this.os = os;
return this;
}
public Computer create() {
Computer c = new LenovoComputer();
c.setBoard(board);
c.setMemory(memory);
c.setCpu(cpu);
c.setFan(fan);
c.setOs(os);
return c;
}
}
}
Builder以一个内部类的形式存在,产品的创建行为和创建过程都在builder中完成了,这种写法方式相比标准的builder模式来说更加的简洁了。
所以我们在开发的时候使用设计模式时需要灵活的变通,不要死死的按照标准的模式来开发。