建造者模式
建造者模式又叫生成器模式,它目的是将复杂的对象创建过程抽象成具体的建造者,而具体的实现过程用户无需关心。
前言
建造者模式一般分成四个角色,产品、抽象建造者、具体建造者、指挥者
一、具体类图
以建造一个构造移动房子为例,构建房子需要墙、窗户、房顶
而房子可能有高的矮的,需要的窗户数量和墙的数量也不同,这是建造者模式就是一个非常好的选择。
二、具体代码
创建产品类房子
public class House {
Roof roof;
List<Wall> walls;
List<Window> windows;
public House() {
this.walls = new ArrayList<Wall>();
this.windows = new ArrayList<Window>();
}
public House addRoof(Roof roof) {
this.roof = roof;
return this;
}
public House addWall(Wall wall) {
this.walls.add(wall);
return this;
}
public House addWindow(Window window) {
this.windows.add(window);
return this;
}
}
依次创建墙、窗户、房顶实体类
public class Wall {
String name;
String material;
public Wall(String material) {
this.name = "Wall made out of " + material;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
public class Window {
String name;
String material;
public Window(String material) {
this.name = "Window made out of " + material;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
public class Roof {
String name;
String material;
public Roof(String material) {
this.name = "Roof made out of " + material;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
创建抽象建造者HousBuilder
public abstract class HouseBuilder {
House house = new House();
public abstract HouseBuilder addWalls();
public abstract HouseBuilder addRoof();
public abstract HouseBuilder addWindows();
public House build() {
return house;
}
}
创建实际建造者如StoneHouseBuilder 和 WoodHouseBuilder
public class StoneHouseBuilder extends HouseBuilder {
int numWalls = 5; // Stone houses have 5 walls: 4 exterior, 1 interior
int numWindows = 20; // Stone houses have a lot of windows
String windowMaterial = "Antique glass";
String interiorWallMaterial = "Stone, 1 foot thick";
String roofMaterial = "Tile";
public StoneHouseBuilder() {
}
public HouseBuilder addWalls() {
// Add 4 exterior walls
for (int i = 0; i < numWalls-1; i++) {
house.addWall(new Wall(interiorWallMaterial));
}
return this;
}
public HouseBuilder addWindows() {
for (int i = 0; i < numWindows; i++) {
house.addWindow(new Window(windowMaterial));
}
return this;
}
public HouseBuilder addRoof() {
house.addRoof(new Roof(roofMaterial));
return this;
}
public House build() {
System.out.println("Stick everything together with mortar");
return house;
}
}
public class WoodHouseBuilder extends HouseBuilder {
int numWindows = 10;
String windowMaterial = "Tempered glass";
String wallMaterial = "Wood, 4x6";
String roofMaterial = "Metal";
public WoodHouseBuilder() {
}
public HouseBuilder addWalls() {
// add exterior walls
for (int i = 0; i < 4; i++) {
System.out.println("Nailing wood for wall made out of " + wallMaterial);
house.addWall(new Wall(wallMaterial));
}
return this;
}
public HouseBuilder addWindows() {
for (int i = 0; i < numWindows; i++) {
System.out.println("Adding window made out of " + windowMaterial);
house.addWindow(new Window(windowMaterial));
}
return this;
}
public HouseBuilder addRoof() {
house.addRoof(new Roof(roofMaterial));
return this;
}
public House build() {
System.out.println("Nail everything together");
return house;
}
}
创建具体指挥者
public class HouseDirector {
public static void main(String[] args) {
HouseBuilder woodHouseBuilder = new WoodHouseBuilder();
House woodHouse = woodHouseBuilder.addWalls().addWindows().addRoof().build();
System.out.println(woodHouse);
HouseBuilder stoneHouseBuilder = new StoneHouseBuilder();
House stoneHouse = stoneHouseBuilder.addWalls().addWindows().addRoof().build();
System.out.println(stoneHouse);
}
}
总结
(1)建造者模式下使用者不需要知道整个产品的内部构造,将产品本身和产品创建进行解耦,使得相同的创建过程可以创建不同的产品对象。
(2)每个具体的建造者之间相互独立互不影响,可以充分利用这层作用实现自身建造过程。