1.建造者模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程,可以创建不同的表示。
2.建造者模式通用类图,如图
3.例子是为了实现,客户启动一辆车时,按照不同的顺序启动车的各个功能。
在建造者模式中有4种角色
(1)Product 产品类
CarModel,通常是实现了模板方法模式
(2) Builder抽象建造者
CarBuilder,规范产品的组建
(3)ConcreteBuilder 具体建造者
(4)Director 导演类
负责安排已有模块的顺序
4.建造者模式
(1)优点:
封装性
易扩展
便于控制细节风险
(2)使用场景
a. 相同的方法,不同的执行顺序,产生不同的事件结果时,可以使用建造者模式
b. 多个部件或零件,都可以装配到一个对象,但是产生的运行结果又不相同时,
c. 产品类非常复杂
5.代码实例
(1) 抽象产品类
package com.design.建造者模式; import java.util.ArrayList; import java.util.List; public abstract class CarModel { public final static String CAR_START = "start"; public final static String CAR_STOP = "stop"; public final static String CAR_ALARM = "alarm"; // 启动序列 private List<String> sequence = new ArrayList<String>(); protected abstract void start(); protected abstract void alarm(); protected abstract void stop(); final public void setSequence(List<String> sequence){ this.sequence = sequence; } final public void run(){ for(int i = 0 ; i < sequence.size() ; i++){ String actionName = sequence.get(i); if(CAR_START.equals(actionName)){ this.start(); }else if(CAR_STOP.equals(actionName)){ this.alarm(); }else if(CAR_ALARM.endsWith(actionName)){ this.stop(); } } } }
(2) 具体产品类
benz
package com.design.建造者模式; public class CarModelBenz extends CarModel { @Override protected void start() { System.out.println("Benz start ..."); } @Override protected void alarm() { System.out.println("Benz alarm ..."); } @Override protected void stop() { System.out.println("Benz stop ..."); } }
baoma
package com.design.建造者模式; public class CarModelBaoma extends CarModel { @Override protected void start() { System.out.println("Baoma start ..."); } @Override protected void alarm() { System.out.println("Baoma alarm ..."); } @Override protected void stop() { System.out.println("Baoma stop ..."); } }
(3) 抽象建造者
package com.design.建造者模式.建造者; import java.util.List; import com.design.建造者模式.CarModel; public abstract class CarBuilder { // 传入汽车的启动顺序 public abstract void setSequence(List<String> sequence); // 拿到车辆模式 public abstract CarModel getCarModel(); }
(4) 具体建造者
benz
package com.design.建造者模式.建造者; import java.util.List; import com.design.建造者模式.CarModel; import com.design.建造者模式.CarModelBenz; public class BenzBuilder extends CarBuilder { private CarModelBenz bz = new CarModelBenz(); @Override public void setSequence(List<String> sequence) { this.bz.setSequence(sequence); } @Override public CarModel getCarModel() { return this.bz; } }
bmw
package com.design.建造者模式.建造者; import java.util.List; import com.design.建造者模式.CarModel; import com.design.建造者模式.CarModelBaoma; public class BmwBuilder extends CarBuilder { private CarModelBaoma bmw = new CarModelBaoma(); @Override public void setSequence(List<String> sequence) { this.bmw.setSequence(sequence); } @Override public CarModel getCarModel() { return bmw; } }
(5) 导演类
package com.design.建造者模式; import java.util.ArrayList; import java.util.List; import com.design.建造者模式.建造者.BenzBuilder; import com.design.建造者模式.建造者.BmwBuilder; public class Director { private List<String> sequence = new ArrayList<String>(); private BenzBuilder benzBuilder = new BenzBuilder(); private BmwBuilder bmwBuilder = new BmwBuilder(); // benz车,先按喇叭再启动再停止 public CarModelBenz getBenzModel(){ this.sequence.clear(); this.sequence.add(CarModel.CAR_ALARM); this.sequence.add(CarModel.CAR_START); this.sequence.add(CarModel.CAR_STOP); this.benzBuilder.setSequence(sequence); return (CarModelBenz) this.benzBuilder.getCarModel(); } // bmw车,先启动,再按喇叭,再停车 public CarModelBaoma getBmwModel(){ this.sequence.clear(); this.sequence.add(CarModel.CAR_START); this.sequence.add(CarModel.CAR_ALARM); this.sequence.add(CarModel.CAR_STOP); this.bmwBuilder.setSequence(sequence); return (CarModelBaoma) this.bmwBuilder.getCarModel(); } }
(6) 场景类
package com.design.建造者模式; public class Client { public static void main(String[] args) { Director director = new Director(); // benz director.getBenzModel().run(); //bmw director.getBmwModel().run(); } }
代码结构清晰,易于扩展。