给女友讲讲设计模式——建造者模式(JAVA实例)3

版权声明:个人版权所有 https://blog.csdn.net/iAmSoLucky_/article/details/82559401

前言

小的时候,我特别喜欢四驱车,我就不停的攒零花钱,等攒到足够买的起一辆车的时候,就会那所有的钱去买这辆车,但是很快我发现,组装好的车一般都是要比一个一个部件组装贵多了。发现这个秘密之后,我就会去买各种零部件,然后回来组装。
我想通过这个故事引出我们今天的主角,建造者模式。

建造者模式

package builder;

/**
 * 四驱车类,是一个entity
 * @author luckyharry
 *
 */
public class FourWheelDrive {
    private Motor motor;
    private Shell shell;
    private Tire tire;
    public Motor getMotor() {
        return motor;
    }

    public void setMotor(Motor motor) {
        this.motor = motor;
    }
    public Shell getShell() {
        return shell;
    }
    public void setShell(Shell shell) {
        this.shell = shell;
    }
    public Tire getTire() {
        return tire;
    }
    public void setTire(Tire tire) {
        this.tire = tire;
    }
    @Override
    public String toString() {
        return "FourWheelDrive [motor=" + motor + ", shell=" + shell + ", tire=" + tire + "]";
    }


}

我们首先定义了一个四驱车类,里面包含了马达,轮胎,外壳(我们暂且认为四驱车是由这三个零件组成的)。

package builder;

/**
 * 马达类,作为四驱车的一个零部件
 * @author luckyharry
 *
 */
public class Motor {

    private String engine;
    private String housePower;
    public String getEngine() {
        return engine;
    }
    public void setEngine(String engine) {
        this.engine = engine;
    }
    public String getHousePower() {
        return housePower;
    }
    public void setHousePower(String housePower) {
        this.housePower = housePower;
    }
    @Override
    public String toString() {
        return "Motor [engine=" + engine + ", housePower=" + housePower + "]";
    }


}

马达这个类,具有引擎以及马力这两个属性。

package builder;

/**
 * 外壳类,作为四驱车的一个零部件
 * @author luckyharry
 *
 */
public class Shell {

    private String color;
    private Double length;
    private Double width;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public Double getLength() {
        return length;
    }
    public void setLength(Double length) {
        this.length = length;
    }
    public Double getWidth() {
        return width;
    }
    public void setWidth(Double width) {
        this.width = width;
    }
    @Override
    public String toString() {
        return "Shell [color=" + color + ", length=" + length + ", width=" + width + "]";
    }


}

外壳类,具有颜色,长度,宽度,三个属性。

package builder;

/**
 * 轮胎类,作为四驱车的一个零部件。
 * @author luckyharry
 *
 */
public class Tire {

    private String resistance;
    private String size;
    public String getResistance() {
        return resistance;
    }
    public void setResistance(String resistance) {
        this.resistance = resistance;
    }
    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size = size;
    }
    @Override
    public String toString() {
        return "Tire [resistance=" + resistance + ", size=" + size + "]";
    }


}

轮胎类,具有摩擦力,以及轮胎尺寸两个属性。

package builder;

public interface Builder {

    public void buildMotor(Motor motor);

    public void buildShell(Shell shell);

    public void builTire(Tire tire);

    public FourWheelDrive getCar();

}

这是建造者模式的一个中枢,是通过这个类,封装了一个个零部件的。但是需要注意的是,Builder只是把生成马达,外壳,以及轮胎的方法写好了,但是他并没有进行组装。

package builder;

public class BuilderImpl implements Builder {

    private FourWheelDrive fourWheelDrive=new FourWheelDrive();

    @Override
    public void buildMotor(Motor motor) {
        fourWheelDrive.setMotor(motor);
    }

    @Override
    public void buildShell(Shell shell) {
        fourWheelDrive.setShell(shell);
    }

    @Override
    public void builTire(Tire tire) {
        fourWheelDrive.setTire(tire);
    }

    @Override
    public FourWheelDrive getCar() {
        System.out.println(fourWheelDrive);
        return fourWheelDrive;
    }
}

实现了Builder接口,真正赋予Builder的各个部件,但是这也不是一个组装的类。

package builder;

public class Director {

    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    public void create(Motor motor,Shell shell,Tire tire) {
        builder.buildMotor(motor);
        builder.buildShell(shell);
        builder.builTire(tire);
    }

}

真正组装的类在这呢,那么我们为什么需要Director(导演)这个类呢,因为四驱车一定得需要马达,外壳,轮胎,但是他们的具体属性有可能会不同,所以我们把builder中的各个部件组装在这,调用的人,只需要知道自己想要什么样的四驱车就行,这种操作简直太爽了。

package builder;

public class MainTest {

    public static void main(String[] args) {
        Motor motor=new Motor();
        Shell shell =new Shell();
        Tire tire = new Tire();
        FourWheelDrive car =new FourWheelDrive();
        motor.setEngine("高速引擎");
        motor.setHousePower("大马力");
        shell.setColor("蓝色");
        shell.setLength(80.00);
        shell.setWidth(35.66);
        tire.setResistance("60N");
        tire.setSize("30*30");
        car.setMotor(motor);
        car.setShell(shell);
        car.setTire(tire);
        Builder builder=new BuilderImpl();
        Director director=new Director(builder);
        director.create(motor, shell, tire);
        builder.getCar();
    }

}

上面就是我们动手组装四驱车的时候了,給他赋予自己想要的参数,这样我们就获得了我们想要的四驱车。小时候就是因为这样的一个聪明的举动,所以通过不停地更换零件,感觉自己拥有的每天都是新的。哈哈。

后记

我会一直以这样通俗易懂的自己能想到的例子,讲解设计模式,最大的启发是来源于《大话设计模式》。非常感觉这本书的作者程杰先生。

猜你喜欢

转载自blog.csdn.net/iAmSoLucky_/article/details/82559401
今日推荐