创建者模式

/**
 * 建造者模式:一般用于复杂的对象创建,例如一个对象的多个属性都是对象,或者对象的属性太多,使用构造器初始化,参数太长,容易写错哪个参数是什么意思
 * 角色:产品--产品创建者--指挥者
 * 产品:如自行车:包含坐垫,轮胎等
 * 创建者:组装自行车:步骤组装坐垫,组装轮胎,最后返回产品对象
 * 指挥者:创建者组装坐垫,轮胎等,至少要有个先后顺序吧,先组装哪个,接着组装哪个
 * 下面的例子以生产自行车为例,自行车有坐垫和轮胎2个对象,分别使用不同的创建者组装相应的自行车(摩拜和小蓝)
 *
 *
 */

//行车车对象
public class Bike {
    private ISeat seat; //坐垫对象
    private Ityre ityre; //轮胎对象

    public ISeat getSeat() {
        return seat;
    }

    public void setSeat(ISeat seat) {
        this.seat = seat;
    }

    public Ityre getItyre() {
        return ityre;
    }

    public void setItyre(Ityre ityre) {
        this.ityre = ityre;
    }
}
//坐垫接口
public interface ISeat {
    void seat();
}
//黑色的坐垫
public class BlackSeat implements ISeat {
    @Override
    public void seat() {
        System.out.println("这是黑色的坐垫");
    }
}
//灰色的坐垫
public class GreySeat implements ISeat {
    @Override
    public void seat() {
        System.out.println("这是灰色的坐垫");
    }
}
//轮胎接口
public interface Ityre {
    void tyre();
}
//大轮胎
public class BigTyre implements Ityre {
    @Override
    public void tyre() {
        System.out.println("这是大的轮胎");
    }
}
//小轮胎
public class SmallTyre implements Ityre {
    @Override
    public void tyre() {
        System.out.println("这是小的轮胎");
    }
}
//自行车组装接口
public interface BikeBuilder{
    void buildSeat();//组装坐垫
    void buildTyre();//组装轮胎
    Bike createBike();//组装好的自行车
}
//摩拜自行车组装者
public class MobikeBuilder implements BikeBuilder {
    private Bike bike=new Bike();
    @Override
    public void buildSeat() {
        bike.setSeat(new BlackSeat());
    } //黑色的坐垫

    @Override
    public void buildTyre() {
        bike.setItyre(new BigTyre());
    }//大的轮胎

    @Override
    public Bike createBike() {
        return bike; //组装好的自行车
    }
}
//小蓝自行车组装者
public class BlugogoBikeBuilder implements BikeBuilder {
    private Bike bike=new Bike();
    @Override
    public void buildSeat() {
        bike.setSeat(new GreySeat());
    } //灰色的坐垫

    @Override
    public void buildTyre() {
        bike.setItyre(new SmallTyre());
    }//小的轮胎

    @Override
    public Bike createBike() {
        return bike;
    }
}
//指挥者,安排组装的顺序,(指挥者也可以省略,将其代码合并到创建者中)
public class Director {
    private BikeBuilder bikeBuilder;

    public Director(BikeBuilder bikeBuilder) {
        this.bikeBuilder = bikeBuilder;
    }
    public Bike builBike(){
        bikeBuilder.buildSeat();//先组装坐垫
        bikeBuilder.buildTyre();//后组装轮胎
        return bikeBuilder.createBike();//最后才返回组装的对象
    }
}

//测试
public class Test {
    public static void main(String[] args) {
        //创建摩拜自行车
        Director director = new Director(new MobikeBuilder());
        Bike bike = director.builBike();
        bike.getItyre().tyre();
        bike.getSeat().seat();
        //创建小蓝自行车
        Director director2 = new Director(new BlugogoBikeBuilder());
        director2.builBike().getSeat().seat();
        director2.builBike().getItyre().tyre();
    }
}

 

posted @ 2019-05-22 18:38  yangxiaohui227  阅读(124)  评论(0编辑  收藏  举报