-
Separate the construction of a complex object from its representation so that the sameconstruction process can create different representations.(将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。)
建造者模式通用类图
image.png
看上面图可能有点懵,举一个简单的例子
假如现在有一个生产电脑代工厂,可能会生产很多电脑品牌的组装电脑,不同的电脑品牌组装电脑的顺序可能不一样。
假如现在要为两个电脑品牌(联想、华为)组装电脑
这些部件假如只有显示器、键盘、鼠标、硬盘
需求就是这样,为不同电脑品牌组装顺序不一样,但是都提供一个公共的方法assemble()
需求有点复杂我们现在看具体代码
在ComputerModel中我们定义了一个setSequence方法,组装点的这几个动作要如何排布,是在这个ArrayList中定义的。然后assemble()方法根据sequence定义的顺序完成指定的顺序动作
首先创建ComputerModel
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author shuliangzhao
* @Title: ComputerModel
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:40
*/
public abstract class ComputerModel {
//sequence控制组装顺序
private List<String> sequence = new ArrayList<>();
protected abstract void productScreen();
protected abstract void productMouse();
protected abstract void productKeyboard();
protected abstract void productHardDisk();
public void assemble() {
for (String str:sequence) {
if (StringUtils.equals(str,"screen")) {
this.productScreen();
}else if (StringUtils.equals(str,"mouse")) {
this.productMouse();
}else if (StringUtils.equals(str,"keyboard")) {
this.productKeyboard();
}else if (StringUtils.equals(str,"hardDisk")) {
this.productHardDisk();
}
}
}
public void setSequence(List<String> sequence) {
this.sequence = sequence;
}
}
LenovoComputer
/**
* @author shuliangzhao
* @Title: LenovoComputer
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:49
*/
public class LenovoComputer extends ComputerModel {
@Override
protected void productScreen() {
System.out.println("联想电脑组装屏幕");
}
@Override
protected void productMouse() {
System.out.println("联想电脑组装鼠标");
}
@Override
protected void productKeyboard() {
System.out.println("联想电脑组装键盘");
}
@Override
protected void productHardDisk() {
System.out.println("联想电脑组装硬盘");
}
}
HuaweiComputer
/**
* @author shuliangzhao
* @Title: HuaweiComputer
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:51
*/
public class HuaweiComputer extends ComputerModel {
@Override
protected void productScreen() {
System.out.println("华为电脑组装屏幕");
}
@Override
protected void productMouse() {
System.out.println("华为电脑组装鼠标");
}
@Override
protected void productKeyboard() {
System.out.println("华为电脑组装键盘");
}
@Override
protected void productHardDisk() {
System.out.println("华为电脑组装硬盘");
}
}
ComputerBuilder
/**
* @author shuliangzhao
* @Title: ComputerBuilder
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:51
*/
public abstract class ComputerBuilder {
//建造一个模型,你要给我一个顺序要求,就是组装顺序
public abstract void setSequence(List<String> sequence);
//设置完毕顺序后,就可以直接拿到这个电脑组装顺序
public abstract ComputerModel getComputerModel();
}
HuaweiBuilder
/**
* @author shuliangzhao
* @Title: HuaweiBuilder
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:56
*/
public class HuaweiBuilder extends ComputerBuilder {
private HuaweiComputer huaweiComputer = new HuaweiComputer();
@Override
public void setSequence(List<String> sequence) {
this.huaweiComputer.setSequence(sequence);
}
@Override
public ComputerModel getComputerModel() {
return this.huaweiComputer;
}
}
LenovoBuilder
/**
* @author shuliangzhao
* @Title: LenovoBuilder
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:54
*/
public class LenovoBuilder extends ComputerBuilder {
private LenovoComputer lenovoComputer = new LenovoComputer();
@Override
public void setSequence(List<String> sequence) {
lenovoComputer.setSequence(sequence);
}
@Override
public ComputerModel getComputerModel() {
return this.lenovoComputer;
}
}
场景类Director
/**
* 场景类可以根据不同场景组装电脑
* @author shuliangzhao
* @Title: Director
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 22:57
*/
public class Director {
private List<String> sequence = new ArrayList<>();
private HuaweiBuilder huaweiBuilder = new HuaweiBuilder();
private LenovoBuilder lenovoBuilder = new LenovoBuilder();
//组装华为A系列先屏幕、鼠标、键盘、硬盘
public HuaweiComputer getAHuaweiComputer() {
this.sequence.clear();
sequence.add("screen");
sequence.add("mouse");
sequence.add("keyboard");
sequence.add("hardDisk");
this.huaweiBuilder.setSequence(sequence);
return (HuaweiComputer) this.huaweiBuilder.getComputerModel();
}
//组装华为B系列先鼠标、屏幕、硬盘、键盘
public HuaweiComputer getBHuaweiComputer() {
this.sequence.clear();
sequence.add("mouse");
sequence.add("screen");
sequence.add("hardDisk");
sequence.add("keyboard");
this.huaweiBuilder.setSequence(sequence);
return (HuaweiComputer) this.huaweiBuilder.getComputerModel();
}
//组装联想A系列先硬盘、键盘、鼠标、屏幕
public LenovoComputer getALenovoComputer() {
this.sequence.clear();
sequence.add("hardDisk");
sequence.add("keyboard");
sequence.add("mouse");
sequence.add("screen");
this.lenovoBuilder.setSequence(sequence);
return (LenovoComputer) this.lenovoBuilder.getComputerModel();
}
//组装联想B系列先键盘、鼠标、硬盘屏幕
public LenovoComputer getBLenovoComputer() {
this.sequence.clear();
sequence.add("keyboard");
sequence.add("mouse");
sequence.add("hardDisk");
sequence.add("screen");
this.lenovoBuilder.setSequence(sequence);
return (LenovoComputer) this.lenovoBuilder.getComputerModel();
}
}
客户端
/**
* @author shuliangzhao
* @Title: Client
* @ProjectName design-parent
* @Description: TODO
* @date 2019/5/29 23:07
*/
public class Client {
public static void main(String[] args) {
Director director = new Director();
//组装华为A系列10台
for (int i = 0;i<2;i++) {
director.getAHuaweiComputer().assemble();
}
//组装联想B系列10台
for (int i=0;i<2;i++) {
director.getBLenovoComputer().assemble();
}
}
}
运行结果
image.png
-
建造者模式优点:
1.具有封装性,使用建造者模式可以使客户端不必知道产品内部组成的细节。
2.建造者独立,容易扩展
-
建造者模式使用场景:
1.相同的方法,不同的执行顺序,产生不同的事件结果时,可以采用建造者模式
2.产品类非常复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建
造者模式非常合适。
网友评论