设计模式之建造者模式
意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
适用性:当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。 当构造过程必须允许被构造的对象有不同的表示时。
建造者模式通常包括以下这几个角色:
1、Builder:给出一个抽象接口,规范建造者对于生产的产品的各个组成部分的建造。这个接口只是定一个规范,不涉及具体的建造,具体的建造让继承于它的子类(ConcreteBuilder)去实现。
2、ConcreteBuilder:实现builder接口,针对不同的商业逻辑,具体化各对象部分的建造,最后返回一个建造好的产品。
3、Director:导演,顾名思义,负责规范流程之用。在指导中不涉及产品的创建,只负责保证复杂对象各部分被创建或按某种顺序创建。
4、Product:复杂对象。
下面以建造一个车为例子,如图
通过director既能够建一个宝马,也能建一个奥迪,控制流程在director职中,如果将来又要加一个奔驰,只用新增一个奔驰的builder即可,不用做过多改动就能很好的加进去。流程控制都在director中,举例复杂的逻辑构建在builder中。示例代码如下。
/** * Open Free. * Code for Open Community, free to use! */ package designpattern.builder; /** * @author admin * @version $Id Car.java, v 0.1 2015-10-18 下午4:26 admin Exp $$ */ public class Car { /** * 轮子 */ private String wheel; /** * 底盘 */ private String chassis; /** * 车窗 */ private String window; /***** * 其他很多属性。。。 */ private String other; /** * Getter method for property <tt>wheel</tt>. * * @return property value of wheel */ public String getWheel() { return wheel; } /** * Setter method for property <tt>wheel</tt>. * * @param wheel value to be assigned to property wheel */ public void setWheel(String wheel) { this.wheel = wheel; } /** * Getter method for property <tt>chassis</tt>. * * @return property value of chassis */ public String getChassis() { return chassis; } /** * Setter method for property <tt>chassis</tt>. * * @param chassis value to be assigned to property chassis */ public void setChassis(String chassis) { this.chassis = chassis; } /** * Getter method for property <tt>window</tt>. * * @return property value of window */ public String getWindow() { return window; } /** * Setter method for property <tt>window</tt>. * * @param window value to be assigned to property window */ public void setWindow(String window) { this.window = window; } /** * Getter method for property <tt>other</tt>. * * @return property value of other */ public String getOther() { return other; } /** * Setter method for property <tt>other</tt>. * * @param other value to be assigned to property other */ public void setOther(String other) { this.other = other; } @Override public String toString() { return "Car{" + "wheel='" + wheel + '\'' + ", chassis='" + chassis + '\'' + ", window='" + window + '\'' + ", other='" + other + '\'' + '}'; } } /** * Open Free. * Code for Open Community, free to use! */ package designpattern.builder; /** * @author admin * @version $Id Builder.java, v 0.1 2015-10-18 下午4:29 admin Exp $$ */ public interface Builder { /** * 建造轮子 */ public void buildWheel(); /** * 建造底盘 */ public void buildChassis(); /** * 建造窗户 */ public void buildWindow(); /** * 建造其他部件 */ public void buildOther(); /** * 返回构建的产品 * @return */ public Car getCar(); } /** * Open Free. * Code for Open Community, free to use! */ package designpattern.builder; /** * @author admin * @version $Id AudiBuilder.java, v 0.1 2015-10-18 下午4:33 admin Exp $$ */ public class AudiBuilder implements Builder { /** * 要构建的产品 */ private Car car; public AudiBuilder() { this.car = new Car(); } @Override public void buildWheel() { car.setWheel("Audi wheel!"); } @Override public void buildChassis() { car.setChassis("Audi chassis!"); } @Override public void buildWindow() { car.setWindow("Audi window!"); } @Override public void buildOther() { car.setOther("Audi other!"); } /** * Getter method for property <tt>car</tt>. * * @return property value of car */ public Car getCar() { return car; } /** * Setter method for property <tt>car</tt>. * * @param car value to be assigned to property car */ public void setCar(Car car) { this.car = car; } } /** * Open Free. * Code for Open Community, free to use! */ package designpattern.builder; /** * @author admin * @version $Id BmwBuilder.java, v 0.1 2015-10-18 下午4:34 admin Exp $$ */ public class BmwBuilder implements Builder { /** * 要构建的产品 */ private Car car; public BmwBuilder() { this.car = new Car(); } @Override public void buildWheel() { car.setWheel("Bmw wheel!"); } @Override public void buildChassis() { car.setChassis("Bmw chassis!"); } @Override public void buildWindow() { car.setWindow("Bmw window!"); } @Override public void buildOther() { car.setOther("Bmw other!"); } /** * Getter method for property <tt>car</tt>. * * @return property value of car */ public Car getCar() { return car; } /** * Setter method for property <tt>car</tt>. * * @param car value to be assigned to property car */ public void setCar(Car car) { this.car = car; } } /** * Open Free. * Code for Open Community, free to use! */ package designpattern.builder; /** * @author admin * @version $Id Director.java, v 0.1 2015-10-18 下午4:39 admin Exp $$ */ public class Director { private Builder builder; public void constroct(){ builder.buildWindow(); builder.buildChassis(); builder.buildWheel(); builder.buildOther(); } /** * Getter method for property <tt>builder</tt>. * * @return property value of builder */ public Builder getBuilder() { return builder; } /** * Setter method for property <tt>builder</tt>. * * @param builder value to be assigned to property builder */ public void setBuilder(Builder builder) { this.builder = builder; } } /** * Open Free. * Code for Open Community, free to use! */ package designpattern.builder; /** * @author admin * @version $Id Customer.java, v 0.1 2015-10-18 下午4:39 admin Exp $$ */ public class Customer { public static void main(String[] args) { //建一个奥迪Audi Builder builder = new AudiBuilder(); //流程控制师 Director director = new Director(); //注入构建audi的建造 director.setBuilder(builder); director.constroct(); Car car = builder.getCar(); System.out.println(car); //建造一个宝马 builder = new BmwBuilder(); //注入构建bmw的建造 director.setBuilder(builder); director.constroct(); car = builder.getCar(); System.out.println(car); } }
posted on 2015-10-18 17:02 NewPanderKing 阅读(323) 评论(1) 编辑 收藏 举报