首页 > 代码库 > 第七讲:建造者模式

第七讲:建造者模式

技术分享

技术分享

 技术分享

技术分享

Product:House

Builder:HouseBuilder(工程队)

ConcreteBuilder:PingFangBuilder、GongyuBuilder

Director:组合的模式,包含Builder,包含工程队,由Director来指使工程队做事.组合不是聚合,Director包含了Builder.

技术分享

construct()是建造方法,不是构造函数.就是Director的makeHouse()方法.

技术分享

演变的过程:自己做->工程队做->设计师做.

客户端代码越来越少越来越简洁.


 技术分享

工厂:GongyuBuidler、PingFangBuilder


 

public class GongyuBuilder implements HouseBuilder{
    House house = new House();
    @Override
    public void makeFloor() {
        // TODO Auto-generated method stub
        house.setFloor("公寓-->地板");
    }

    @Override
    public void makeWall() {
        // TODO Auto-generated method stub
        house.setWall("公寓-->墙");
    }

    @Override
    public void makeHousetop() {
        // TODO Auto-generated method stub
        house.setHousetop("公寓-->房顶");
    }

    @Override
    public House getHouse() {
        // TODO Auto-generated method stub
        return house;
    }
     
}
/*
 * 房子
 * 
 */
public class House {
      //地板
    private String floor;//复合属性可能是其它类的对象.这里做简单一些
      //
    private String wall;
      //屋顶
    private String housetop;
    public String getFloor() {
        return floor;
    }
    public void setFloor(String floor) {
        this.floor = floor;
    }
    public String getWall() {
        return wall;
    }
    public void setWall(String wall) {
        this.wall = wall;
    }
    public String getHousetop() {
        return housetop;
    }
    public void setHousetop(String housetop) {
        this.housetop = housetop;
    }

       

}
//工人有很多种.有些是修公寓的,有些是修其他的.
/*
 *  工程队 
 */
public interface HouseBuilder {
     //修地板
    public void makeFloor();
    // 修墙
    public void makeWall();
    //修屋顶
    public void makeHousetop();
    
    public House getHouse();
}
public class HouseDirector {
   //private HouseBuilder builder;
   
/*public HouseDirector(HouseBuilder builder) {
    super();
    this.builder = builder;
}*/
   //有些书籍不在HouseDirector的构造函数中传递buidler(工程队)的引用
//public void makeHouse(){
    public void makeHouse(HouseBuilder builder){//第二种方式在方法中传递builder
    //第二种方式更加规范,第一种方式不是错的,但是耗费资源,因为builder完全没有必要做成一个属性.
    //在方法里面做接收builder就可以了,做成属性比较耗资源.
        
        /*this.builder.makeFloor();
    this.builder.makeHousetop();
    this.builder.makeWall();*/
        builder.makeFloor();
        builder.makeHousetop();
        builder.makeWall();
}
/*public HouseBuilder getBuilder() {
    return builder;
}

public void setBuilder(HouseBuilder builder) {
    this.builder = builder;
}
  */ 
}
//客户端要修建一个房子
public class MainClass {
    public static void main(String[] args) {
        //客户直接修造房子
/*        House house = new House();
        house.setFloor("地板");
        house.setWall("墙");
        house.setHousetop("屋顶");*/
        //原本是修平房,现在改成修公寓了,要把这些属性都改掉.
        //这种方式的话又要直接修改客户端,而且对象的创建细节全部放在客户那里.
        //客户亲自去修.
        
        //一般是:客户->开发商->工人
        
        //由工程队来修
        //HouseBuilder builder = new PingFangBuilder();
        HouseBuilder builder = new GongyuBuilder();
        //调用工程队来修.
/*builder.makeFloor();
builder.makeWall();
builder.makeHousetop();*/
        //由设计者调用工程队来做
        //HouseDirector director = new HouseDirector(builder);
        HouseDirector director = new HouseDirector();
        //以后学了反射之后动态控制加载哪一个工程队,更加方便了,客户端无需修改了.
        //只需要添加一个新的builder就可以了.
        //director.makeHouse();
        director.makeHouse(builder);
        House house = builder.getHouse();
System.out.println(house.getFloor());
System.out.println(house.getWall());
System.out.println(house.getHousetop());
    }
}
//修平房的工人,实现修房子这个接口.
/*
 * 平房工程队
 */
public class PingFangBuilder implements HouseBuilder{
    House house = new House();
    @Override
    public void makeFloor() {
        // TODO Auto-generated method stub
        house.setFloor("平房-->地板");//builder建筑者为你修房子
    }

    @Override
    public void makeWall() {
        // TODO Auto-generated method stub
        house.setWall("平房-->墙");
    }

    @Override
    public void makeHousetop() {
        // TODO Auto-generated method stub
        house.setHousetop("平房-->房顶");
    }

    @Override
    public House getHouse() {
        // TODO Auto-generated method stub
        return house;
    }
     
}

 

第七讲:建造者模式