设计模式4 - 构建者模式Builder Design Pattern

来源:互联网 发布:卖家加入淘宝客的条件 编辑:程序博客网 时间:2024/04/19 01:35


1 Introduction

Builder pattern is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.


Complex Object Construction

For example, you can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction and so on roof construction. Finally the whole home object is returned. Here using the same process you can build houses with different properties.

GOF says,

“Separate the construction of a complex object from its representation so that the same construction process can create different representations” [GoF 94]

What is the difference between abstract factory and builder pattern?

Abstract factory may also be used to construct a complex object, then what is the difference with builder pattern? In builder pattern emphasis is on ‘step by step’. Builder pattern will have many number of small steps. Those every steps will have small units of logic enclosed in it. There will also be a sequence involved. It will start from step 1 and will go on upto step n and the final step is returning the object. In these steps, every step will add some value in construction of the object. That is you can imagine that the object grows stage by stage. Builder will return the object in last step. But in abstract factory how complex the built object might be, it will not have step by step object construction.


2 Sample Java Source Code for Builder Pattern

Following is the interface, that will be returned as the product from the builder.

public interface HousePlan {    public void setBasement(String basement);    public void setStructure(String structure);    public void setRoof(String roof);    public void setInterior(String interior);} 

Concrete class for the above interface. The builder constructs an implementation for the following class.

public class House implements HousePlan {    private String basement;    private String structure;    private String roof;    private String interior;    public void setBasement(String basement) {        this.basement = basement;    }    public void setStructure(String structure) {        this.structure = structure;    }    public void setRoof(String roof) {        this.roof = roof;    }    public void setInterior(String interior) {        this.interior = interior;                                                                }}

Builder interface. We will have multiple different implementation of this interface in order to facilitate, the same construction process to create different representations.

public interface HouseBuilder {                                                                  public void buildBasement();    public void buildStructure();    public void buildRoof();    public void buildInterior();    public House getHouse();}

First implementation of a builder.

public class IglooHouseBuilder implements HouseBuilder {    private House house;    public IglooHouseBuilder() {        this.house = new House();    }    public void buildBasement() {        house.setBasement("Ice Bars");    }    public void buildStructure() {        house.setStructure("Ice Blocks");    }    public void buildRoof() {        house.setRoof("Ice Dome");    }    public void buildInterior() {        house.setInterior("Ice Carings");    }    public House getHouse() {        return this.house;    }                                                                                        }  


Second implementation of a builder. Tipi is a type of eskimo house.

public class TipiHouseBuilder implements HouseBuilder {    private House house;    public TipiHouseBuilder() {        this.house = new House();    }    public void buildBasement() {        house.setBasement("Wooden Poles");    }    public void buildStructure() {        house.setStructure("Wood and Ice");    }    public void buildRoof() {        house.setRoof("Wood, caribou and seal skins");    }    public void buildInterior() {        house.setInterior("Fire Wood");    }    public House getHouse() {                                                                        return this.house;    }}

Following class constructs the house and most importantly, this maintains the building sequence of object.

public class CivilEngineer {    private HouseBuilder houseBuilder;    public CivilEngineer(HouseBuilder houseBuilder) {        this.houseBuilder = houseBuilder;    }    public House getHouse() {        return this.houseBuilder.getHouse();    }    public void constructHouse() {        this.houseBuilder.buildBasement();                                                           this.houseBuilder.buildStructure();        this.houseBuilder.buildRoof();        this.houseBuilder.buildInterior();    }}

Testing the sample builder design pattern.

public class BuilderSample {    public static void main(String[] args) {        HouseBuilder iglooBuilder = new IglooHouseBuilder();        CivilEngineer engineer = new CivilEngineer(iglooBuilder);        engineer.constructHouse();        House house = engineer.getHouse();                                                           System.out.println("Builder constructed: " + house);    }}


原创粉丝点击