title: 建造者模式
author: rack-leen
avatar: /images/favicon.png
authorDesc: 脱发典范
comments: true
copyright: true
date: 2019-5-19 00:00:00
tags:
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
现在客户需要订购东风,奔驰,宝马三种汽车各100辆。四种车的生产顺序是一样的。
客户的需求给指挥者,指挥者将这四种车分别分配给四个建造者建造。
/**
* 建造者模式的这个抽象类有模板方法模式的影子
*/
public abstract class Car {
private ArrayList<String> sequence = new ArrayList<>();
protected abstract void start();
protected abstract void stop();
protected abstract void alarm();
protected abstract void engineBoom();
/**
* 加了final关键字表示run方法和setSequense方法都不能被继承或重写
*
* run方法就是模板方法模式中的功能一样,这个更灵活,能自定义执行顺序
*/
final public void run(){
for (int i=0 ; i<sequence.size() ; i++){
String name = this.sequence.get(i);
if ("start".equals(name)){
this.start();
}else if ("stop".equalsIgnoreCase(name)){
this.stop();
}else if ("alarm".equalsIgnoreCase(name)){
this.alarm();
}else if ("engineBoom".equalsIgnoreCase(name)){
this.engineBoom();
}
}
}
/**
* 从外界传进入执行顺序
* @param sequence
*/
final public void setSequence(ArrayList<String> sequence){
this.sequence = sequence ;
}
}
public class Benz extends Car {
@Override
protected void start() {
System.out.println("奔驰是这样启动的");
}
@Override
protected void stop() {
System.out.println("奔驰是这样停下来的");
}
@Override
protected void alarm() {
System.out.println("奔驰的喇叭声音是这样的");
}
@Override
protected void engineBoom() {
System.out.println("奔驰的引擎声音是这样的");
}
}
宝马汽车
public class BMW extends Car {
@Override
protected void start() {
System.out.println("宝马是这样启动的");
}
@Override
protected void stop() {
System.out.println("宝马是这样停下来的");
}
@Override
protected void alarm() {
System.out.println("宝马的喇叭声音是这样的");
}
@Override
protected void engineBoom() {
System.out.println("宝马的引擎声音是这样的");
}
}
东风汽车
public class Dongfeng extends Car{
@Override
protected void start() {
System.out.println("东风是这样启动的");
}
@Override
protected void stop() {
System.out.println("东风是这样停下来的");
}
@Override
protected void alarm() {
System.out.println("东风的喇叭声音是这样的");
}
@Override
protected void engineBoom() {
System.out.println("东风的引擎声音是这样的");
}
}
public class BenzBuilder extends CarBuilder {
/* 这里先创建一个产品 */
private Benz benz = new Benz();
/*然后为产品设置制造顺序*/
@Override
public void setSequense(ArrayList<String> sequense) {
this.benz.setSequence(sequense);
}
/* 产品创建完成就返回这个产品 */
@Override
public Car getCar() {
return this.benz;
}
}
宝马汽车的建造者
public class BMWBuilder extends CarBuilder {
private BMW bmw = new BMW();
@Override
public void setSequense(ArrayList<String> sequense) {
this.bmw.setSequence(sequense);
}
@Override
public Car getCar() {
return this.bmw;
}
}
东风汽车的建造者
public class DongFengBuilder extends CarBuilder {
private Dongfeng dongfeng = new Dongfeng();
@Override
public void setSequense(ArrayList<String> sequense) {
this.dongfeng.setSequence(sequense);
}
@Override
public Car getCar() {
return this.dongfeng;
}
}
/**
* 这是所有建造者的指挥者,它来只会建造者建造产品
*/
public class Director {
private ArrayList<String> sequense = new ArrayList<>();
private BenzBuilder benzBuilder = new BenzBuilder();
private BMWBuilder bmwBuilder = new BMWBuilder();
private DongFengBuilder dongFengBuilder = new DongFengBuilder();
/**
* 宝马模型
*/
public BMW getBMW(){
this.sequense.clear();
this.sequense.add("start");
this.sequense.add("stop");
this.bmwBuilder.setSequense(this.sequense);
return (BMW) this.bmwBuilder.getCar();
}
/**
* 奔驰模型
*/
public Benz getBenz(){
this.sequense.clear();
this.sequense.add("start");
this.sequense.add("alarm");
this.sequense.add("stop");
this.benzBuilder.setSequense(this.sequense);
return (Benz) this.benzBuilder.getCar();
}
/**
* 东风模型
*/
public Dongfeng getDongFeng(){
this.sequense.clear();
this.sequense.add("start");
this.sequense.add("engineBoom");
this.sequense.add("stop");
this.dongFengBuilder.setSequense(this.sequense);
return (Dongfeng) this.dongFengBuilder.getCar();
}
}
public class Test {
public static void main(String[] args) {
Director director = new Director();
/**
* 我们这里是通过指挥者操作建造者建造的产品实例
*/
/* 奔驰建造 */
for (int i=0 ; i<100 ; i++){
director.getBenz().run();
}
/* 宝马建造 */
for (int i=0 ; i<100 ; i++){
director.getBMW().run();
}
/* 东风建造 */
for (int i=0 ; i<100 ; i++){
director.getDongFeng().run();
}
}
}