设计模式:建造者模式
程序员文章站
2022-03-27 07:58:16
...
定义
建造者模式:将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以构建不同的表示
Builder Pattern: Separate the construction of a complex object from its representation so that the same construction process can create different representations
优点
- 封装性,可以使客户端不必知道产品内部组成细节
- 建造者独立,容易扩展
- 便于控制细节风险
使用场景
- 相同的方法,不同的执行顺序,产生不同的时间结果时
- 多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不相同时
- 产生类非常复杂,或者产品类中的调用顺序不同产生了不同的效能
实现方法
Product产品类:通常实现了模板方法模式
Builder抽象建造者:规范产品的组建,一般由子类实现
ConcreteBuilder具体建造者:实现抽象类定义的所有方法,并返回一个建造好的对象
Director导演类:负责安排已有模块的顺序,然后告诉Builder开始建造
具体实现
产品抽象类
public abstract class CarModel { private ArrayList<String> sequence = new ArrayList<String>(); protected abstract void start(); protected abstract void stop(); protected abstract void alarm(); protected abstract void engineBoom(); final public void run() { for (int i = 0; i < sequence.size(); i++) { String actionName = this.sequence.get(i); if (actionName.equalsIgnoreCase("start")) { this.start(); } else if (actionName.equalsIgnoreCase("stop")) { this.stop(); } else if (actionName.equalsIgnoreCase("alarm")) { this.alarm(); } else if (actionName.equalsIgnoreCase("engine boom")) { this.engineBoom(); } } } final public void setSequence(ArrayList<String> sequence) { this.sequence = sequence; } }
产品实现类A
public class BenzModel extends CarModel { @Override protected void start() { System.out.println("Benz Start!"); } @Override protected void stop() { System.out.println("Benz Stop!"); } @Override protected void alarm() { System.out.println("Benz Alarm!"); } @Override protected void engineBoom() { System.out.println("Benz Engine Boom!"); } }
产品实现类B
public class BMWModel extends CarModel { @Override protected void start() { System.out.println("BMW Start!"); } @Override protected void stop() { System.out.println("BMW Stop!"); } @Override protected void alarm() { System.out.println("BMW Alarm!"); } @Override protected void engineBoom() { System.out.println("BMW Engine Boom!"); } }
抽象建造者
public abstract class CarBuilder { public abstract void setSequence(ArrayList<String> sequence); public abstract CarModel getCarModel(); }
具体建造者A
public class BenzBuilder extends CarBuilder { private BenzModel benz = new BenzModel(); @Override public void setSequence(ArrayList<String> sequence) { this.benz.setSequence(sequence); } @Override public CarModel getCarModel() { return benz; } }
具体建造者B
public class BMWBuilder extends CarBuilder { private BMWModel bmw = new BMWModel(); @Override public void setSequence(ArrayList<String> sequence) { this.bmw.setSequence(sequence); } @Override public CarModel getCarModel() { return bmw; } }
导演类
public class Director { private ArrayList<String> sequence = new ArrayList<String>(); private BenzBuilder benzBuilder = new BenzBuilder(); private BMWBuilder bmwBuilder = new BMWBuilder(); public BenzModel getABenzModel() { this.sequence.clear(); this.sequence.add("start"); this.sequence.add("stop"); this.benzBuilder.setSequence(sequence); return (BenzModel) this.benzBuilder.getCarModel(); } public BenzModel getBBenzModel() { this.sequence.clear(); this.sequence.add("engine boom"); this.sequence.add("start"); this.sequence.add("stop"); this.benzBuilder.setSequence(sequence); return (BenzModel) this.benzBuilder.getCarModel(); } public BMWModel getABMWModel() { this.sequence.clear(); this.sequence.add("start"); this.sequence.add("stop"); this.bmwBuilder.setSequence(sequence); return (BMWModel) this.bmwBuilder.getCarModel(); } public BMWModel getBBMWModel() { this.sequence.clear(); this.sequence.add("engine boom"); this.sequence.add("start"); this.sequence.add("stop"); this.bmwBuilder.setSequence(sequence); return (BMWModel) this.bmwBuilder.getCarModel(); } }
场景类
public class Client { public static void main(String[] args) { Director director = new Director(); for (int i = 0; i < 2; i++) { director.getABenzModel().run(); } for (int i = 0; i < 3; i++) { director.getBBenzModel().run(); } for (int i = 0; i < 2; i++) { director.getABMWModel().run(); } for (int i = 0; i < 3; i++) { director.getBBMWModel().run(); } } }
PS:基本理解这种写法的思想和意义,但不深刻
参考内容:
《设计模式之禅》秦小波