设计模式-行为型模式-创建者模式
Introduce:
创建者模式属于创建型模式,其核心思想是将一个复杂对象的构建算法与它的构成部件、组装方式分离,使得构建算法和组装方式可以独立应对变化,那么部件可以被复用,组装方式可以适应不同的变化,不同的构建算法可以得到不一样的结果。创建者模式主要由5部分构成:Abstrct Builder, Concrete Builder, Abstract Product, Concrete Product and Director。
我们可以举个例子,盖房子。房子是Abstract Product,洋房、别墅、茅草屋...具体的Product;工人是Abstract Builder,瓦匠工、木工、打地基的工匠、铺楼板的工匠...具体的Builder,这些工匠都有一技之长,或者好几手绝活,但是要是让他们自己独立盖一幢房子,他们是不知道怎么盖的,他们只能干自己特长的事,但是Director知道怎么盖房子(什么样的房子怎么盖他都会),因为他上过学,纸上的功夫那是刚刚的,但是呢你要让他打地基,他干不了,你要让他搬砖,他说“他会”(程序员不都是搬砖吗),结果被农民工笑掉大牙;有人要盖房,什么样的需求,给Director一说,他就开始吩咐各类工人干活,谁先干(当然是先打地基,旧房改造不需要打地基),谁接着干,这都是有策略的(这里引入策略模式,根据客户的需求去生成对应的产品)。
Advantages and Disadvantages
优点:在创建者模式中,客户端不用再负责Product的创建与组装,而是将这个Product的创建过程交给具体的创建者,客户端只负责调用对象,这样就能各司其职,Director就只管怎么设计房子,怎么盖;各类民工只要熟练自己的绝活就行。
缺点:如果不能复用Builder,每个Builder被调用的次数少,每个Product差异大,那么就需要很多Builder,增加了代码的复杂性,如果我对现有的Builder的功能做了修改,也同样会影响其他Product。
下面的Demo我只写了一个Builder,具体环境中可以自己把握,在实际项目中我在用创建者模式的同时也引入了策略模式。
具体产品,可做抽象
package BuilderPattern; public class Room { private String foundation; private String window; private String door; private String floor; private String roof; public String getFoundation() { return foundation; } public void setFoundation(String foundation) { this.foundation = foundation; } public String getWindow() { return window; } public void setWindow(String window) { this.window = window; } public String getDoor() { return door; } public void setDoor(String door) { this.door = door; } public String getFloor() { return floor; } public void setFloor(String floor) { this.floor = floor; } public String getRoof() { return roof; } public void setRoof(String roof) { this.roof = roof; } }
抽象创建者
package BuilderPattern; public interface IBuilder { void buildFoundation(); void buildWindow(); void buildDoor(); void buildFloor(); void buildRoof(); Room getRoom(); }
具体创建者
package BuilderPattern; public class Builder implements IBuilder { private Room room; public Builder() { this.room = new Room(); } @Override public void buildFoundation() { System.out.println("Build foundation..."); } @Override public void buildWindow() { System.out.println("Build window..."); } @Override public void buildDoor() { System.out.println("Build door..."); } @Override public void buildFloor() { System.out.println("Build floor..."); } @Override public void buildRoof() { System.out.println("Build roof..."); } @Override public Room getRoom() { return this.room; } }
Director
package BuilderPattern; public class Director { private IBuilder builder; public Director() {} public Director(IBuilder builder) { this.builder = builder; } public void orderBuilder() { if (null != this.builder) { // First build foundation; builder.buildFoundation(); // Second build floor builder.buildFloor(); // Third build door and window builder.buildDoor(); builder.buildWindow(); // Fourth build roof builder.buildRoof(); } } }
调用
package BuilderPattern; public class Main { public static void execBuilderPattern() { System.out.println("Builder Pattern:"); // Builder implements all build method, while they don't know how to build a room. Builder builder = new Builder(); // Director know how to build a room, but him doesn't know how to work. Director director = new Director(builder); director.orderBuilder(); Room room = builder.getRoom(); System.out.println("End---------------------------"); } }