设计模式之创建型模式---建造者模式

建造者模式(Builder)
构建者模式和工厂模式很类似,区别在于构建者模式是一种个性化产品的创建,通过组装零配件而创建一个新产品。而工厂模式是一种标准化的产品创建,不关心产品的构建过程,只关心什么产品是由什么工厂产生的。下面我们来举例汽车的购买,每款汽车都需要车轮,发动机。
1.我们首先写一个车轮和发动机的接口
车轮

public interface Wheel {

    public String getWheelName();

}

发动机

 public interface Engine {

    public String getEngineName();
}

2.接下来我们分别实现这两个接口

/**
 * @author chenglezheng
 * @date 2019-12-22 22:54
 * 米其林轮胎
 */
public class MichelinWheel implements Wheel{

    public String getWheelName() {
        return "MichelinWheel";
    }


}
/**
 * @author chenglezheng
 * @date 2019-12-22 22:56
 * 普利司通轮胎
 */
public class BridgeStoneWheel implements Wheel{

    public String getWheelName() {
        return "BridgeStoneWheel";
    }

}
/**
 * @author chenglezheng
 * @date 2019-12-22 22:58
 * 本田发动机
 */
public class BenTianEngine implements Engine{

    public String getEngineName() {
        return "BenTianEngine";
    }

}
/**
 * @author chenglezheng
 * @date 2019-12-22 22:58
 * 马自达发动机
 */
public class MaZiDaEngine implements Engine{
    public String getEngineName() {
        return "MaZiDaEngine";
    }
}

3.准备好配件后,我们开始组装汽车了,我们先写一个汽车接口

/**
 * @author chenglezheng
 * @date 2019-12-22 22:51
 */
public interface Car {

    public Wheel getWeel();

    public Engine getEngine();

    public String getCarName();

    public float sell();


}

4.接着我们来实现这个接口
组装一辆思域

/**
 * @author chenglezheng
 * @date 2019-12-22 23:09
 * 本田旗下的思域
 */
public class SiYu extends BenTianCar{

    public String getCarName() {
        return "SiYu";
    }

    public float sell() {
        return 120000.00f;
    }
}

组装一辆CX4

/**
 * @author chenglezheng
 * @date 2019-12-22 23:10
 * 马自达旗下的CX4
 */
public class CX4 extends MaZiDaCar{

    public String getCarName() {
        return "CX4";
    }

    public float sell() {
        return 140000.00f;
    }
}

5.组装好以后,开始出售给消费者了

package com.lc.clz.create;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenglezheng
 * @date 2019-12-22 23:20
 */
public class Sell {

    private List<Car> cars=new ArrayList<Car>();

    public void addCar(Car car){
        cars.add(car);
    }

    public float totalPrice(){
        float totalPrice=0.0f;
        for (Car car:cars) {
            totalPrice+=car.sell();
        }
        return totalPrice;
    }

    public void printBill(){
        for (Car car:cars) {
            System.out.print("Car:"+car.getCarName());
            System.out.print(",Wheel :"+car.getWeel().getWheelName());
            System.out.println(",Enige:"+car.getEngine().getEngineName());
            System.out.println("Price:"+car.sell());
        }
    }


}

6.一个汽车商城出售的车子各种各样,这时我们就要建造一个出售的建造者工厂了

/**
 * @author chenglezheng
 * @date 2019-12-22 23:27
 */
public class SellBuilder {

    /**
     * 售出一辆思域
     * @return
     */
    public Sell prepareSellSiYu(){
        Sell buy=new Sell();
        buy.addCar(new SiYu());
        return buy;
    }

  /**
     * 售出一辆CX4
     * @return
     */
    public Sell prepareSellCX4(){
        Sell buy =new Sell();
        buy.addCar(new CX4());
        return buy;
    }

}

7.一切准备就绪,我们开始测试了

/**
 * @author chenglezheng
 * @date 2019-12-22 23:29
 */
public class BuilderTest {

    public static void main(String[] args) {
        SellBuilder sellBuilder=new SellBuilder();

        Sell siYu=sellBuilder.prepareSellSiYu();
        siYu.printBill();

        Sell cx4=sellBuilder.prepareSellCX4();
        cx4.printBill();

    }
}

输出结果:
Car:SiYu,Wheel :MichelinWheel,Enige:BenTianEngine
Price:120000.0
Car:CX4,Wheel :BridgeStoneWheel,Enige:MaZiDaEngine
Price:140000.0

从这以上例子中看出,建造者模式是将很多功能集成到一个类里,这个类可以创造出比较复杂的东西,例如上述汽车从生产到售出的过程。所以与工程模式的区别就是:工厂模式关注的是创建单个产品,而建造者模式则关注创建复合对象,多个部分。因此,是选择工厂模式还是建造者模式,根据实际的业务需求选择对应的模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值