首页 > 编程语言 > 详细

java-設計模式-生成器

时间:2020-09-13 22:00:51      阅读:123      评论:0      收藏:0      [点我收藏+]

生成器模式Bulider

使你能够分步骤创建复杂对象 该模式允许你使用相同的创建代码生成不同类型和形式的对象

 

将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示。

将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,

即产品的组成部分是不变的,但每一部分是可以灵活选择的

 

優點:

各个具体的建造者相互独立,有利于系统的扩展。
客户端不必知道产品内部组成的细节,便于控制细节风险。

缺點:

产品的组成部分必须相同,这限制了其使用范围。
如果产品的内部变化复杂,该模式会增加很多的建造者类。

生成器模式和工厂模式的关注点不同:建造者模式注重零部件的组装过程,而工廠方法模式更注重零部件的创建过程

 

生成器模式結構;

产品角色(Product):它是包含多个组成部件的复杂对象,由具体建造者来创建其各个滅部件。
抽象建造者(Builder):它是一个包含创建产品各个子部件的抽象方法的接口,通常还包含一个返回
复杂产品的方法 getResult()。 具体建造者(Concrete Builder):实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。 指挥者(Director):它调用建造者对象中的部件构造与装配方法完成复杂对象的创建,在指挥者中不涉及具体产品的信息

 

技术分享图片

 

 

簡單的例子

思考如何创建一个 房屋House对象

你需要建造四面墙和地板 安装房门和一套窗户 然后再建造一个屋顶 但是如果你想要

一栋更宽敞更明亮的房屋 还要有院子和其他设施 (例如暖气 排水和供电设备 那又该怎么办呢

 

最簡單的方法擴展house基類

解決:

将对象构造代码从产品类中抽取出来 并将其放在一个名为生成器的独立对象中

技术分享图片

 

 

 

 

 

例子:分佈製造車子:

車子的屬性

/**
 * 車子的特徵1
 */
public class Engine {
    private final double volume;
    private double mileage;
    private boolean started;

    public Engine(double volume,double mileage){
        this.volume = volume;
        this.mileage = mileage;
    }

    public void on(){
        started = true;
    }

    public void off(){
        started = false;
    }

    public boolean isStarted(){
        return started;
    }

    private void go(double mileage){
        if (started){
            this.mileage += mileage;
        }else {
            System.out.println("not go ,you must engine first");
        }

    }

    public double getVolume() {
        return volume;
    }

    public double getMileage() {
        return mileage;
    }
}

 

*
 * 車子特徵2:
 */
public class GPSNavigator {
    private String route;

    public GPSNavigator() {
        this.route = "221b, Baker Street, London  to Scotland Yard, 8-10 Broadway, London";
    }

    public GPSNavigator(String manualRoute) {
        this.route = manualRoute;
    }

    public String getRoute() {
        return route;
    }
}

 

/**
 * 車子的特徵3
 * 擋位類型
 */
public enum  Thransmission {
    SINGLE_SPEED,MANUAL,AUTOMATIC,SEMI_AUTOMATIC
}

 

/**
 * 車子的特徵4
 *
 */
public class TripComputer {
    private Car car;

    public void setCar(Car car) {
        this.car = car;
    }

    public void showFuelLevel(){
        System.out.println("Fuel level:"+car.getFuel());
    }

    public void showStatus(){
        if (this.car.getEngine().isStarted()){
            System.out.println("car started");
        }else{
            System.out.println("car isn‘t started");
        }
    }
}

 

/**
 * 汽車的類型枚舉類
 */
public enum Type {
    CICY_CAR,SPORTS_CAR,SUV
}

 

車子類

public class Car {
    private final Type type;
    private final  int seats;
    private final Engine engine;
    private final Thransmission thransmission;
    private final TripComputer tripComputer;
    private final GPSNavigator gpsNavigator;
    private double fuel = 0;

    public Car(Type type, int seats, Engine engine, Thransmission thransmission, TripComputer tripComputer, GPSNavigator gpsNavigator) {
        this.type = type;
        this.seats = seats;
        this.engine = engine;
        this.thransmission = thransmission;
        this.tripComputer = tripComputer;
        this.gpsNavigator = gpsNavigator;
    }

    public Type getType() {
        return type;
    }

    public int getSeats() {
        return seats;
    }

    public Engine getEngine() {
        return engine;
    }

    public Thransmission getThransmission() {
        return thransmission;
    }

    public TripComputer getTripComputer() {
        return tripComputer;
    }

    public GPSNavigator getGpsNavigator() {
        return gpsNavigator;
    }

    public double getFuel() {
        return fuel;
    }

    public void setFuel(double fuel) {
        this.fuel = fuel;
    }


}

 

生成器:設置車子的屬性。實現接口完成車子創建和手冊創建

public interface Builder {
    void setType(Type type);//汽車的類型
    void setSeats(int seats);
    void setEngine(Engine engine);
    void setThransmission(Thransmission thransmission);
    void setTripComputer(TripComputer tripComputor);
    void setGPSNavigator(GPSNavigator gpsNavigator);

}

 

車子創建:

public class CarBuilder implements Builder{

    private  Type type;
    private  int seats;
    private  Engine engine;
    private  Thransmission thransmission;
    private  TripComputer tripComputer;
    private  GPSNavigator gpsNavigator;

    public void setType(Type type) {
        this.type = type;
    }

    public void setSeats(int seats) {
        this.seats = seats;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public void setThransmission(Thransmission thransmission) {
        this.thransmission = thransmission;
    }

    public void setTripComputer(TripComputer tripComputor) {
        this.tripComputer = tripComputor;
    }

    public void setGPSNavigator(GPSNavigator gpsNavigator) {
        this.gpsNavigator = gpsNavigator;
    }

    /*
    獲取Car的方法
     */
    public Car getResult(){
        return new Car(type,seats,engine,thransmission,tripComputer,gpsNavigator);
    }
}

 

手冊類:

public class Manual {
    private final Type type;
    private final  int seats;
    private final Engine engine;
    private final Thransmission thransmission;
    private final TripComputer tripComputer;
    private final GPSNavigator gpsNavigator;
    private double fuel = 0;

    public Manual(Type type, int seats, Engine engine, Thransmission thransmission, TripComputer tripComputer, GPSNavigator gpsNavigator) {
        this.type = type;
        this.seats = seats;
        this.engine = engine;
        this.thransmission = thransmission;
        this.tripComputer = tripComputer;
        this.gpsNavigator = gpsNavigator;
    }

    public String print(){
        String info = "";
        info += "Type of car: " + type + "\n";
        info += "Count of seats: " + seats + "\n";
        info += "Engine: volume - " + engine.getVolume() + "; mileage - " + engine.getMileage() + "\n";
        info += "Transmission: " + thransmission + "\n";
        if (this.tripComputer != null) {
            info += "Trip Computer: Functional" + "\n";
        } else {
            info += "Trip Computer: N/A" + "\n";
        }
        if (this.gpsNavigator != null) {
            info += "GPS Navigator: Functional" + "\n";
        } else {
            info += "GPS Navigator: N/A" + "\n";
        }
        return info;
    }
}

 

手冊生成器:

public class CarManualBuilder implements Builder {
    private  Type type;
    private  int seats;
    private  Engine engine;
    private  Thransmission thransmission;
    private  TripComputer tripComputer;
    private  GPSNavigator gpsNavigator;

    public void setType(Type type) {
        this.type = type;
    }

    public void setSeats(int seats) {
        this.seats = seats;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public void setThransmission(Thransmission thransmission) {
        this.thransmission = thransmission;
    }

    public void setTripComputer(TripComputer tripComputor) {
        this.tripComputer = tripComputor;
    }

    public void setGPSNavigator(GPSNavigator gpsNavigator) {
        this.gpsNavigator = gpsNavigator;
    }

    /*
    獲取Car手冊的方法
     */
    public Manual getResult(){
        return new Manual(type,seats,engine,thransmission,tripComputer,gpsNavigator);
    }
}

 

建立控制生成器的累:詳單與server

public class Director {
    /**
     * sportCar
     * @param builder
     */
    public void constructSportsCar(Builder builder){
        builder.setType(Type.SPORTS_CAR);
        builder.setSeats(2);
        builder.setEngine(new Engine(3.0, 0));
        builder.setThransmission(Thransmission.SEMI_AUTOMATIC);
        builder.setTripComputer(new TripComputer());
        builder.setGPSNavigator(new GPSNavigator());
    }


    /**
     *city Car
     * @param builder
     */
    public void constructCityCar(Builder builder){
        builder.setType(Type.CICY_CAR);
        builder.setSeats(2);
        builder.setEngine(new Engine(1.2, 0));
        builder.setThransmission(Thransmission.SEMI_AUTOMATIC);
        builder.setTripComputer(new TripComputer());
        builder.setGPSNavigator(new GPSNavigator());
    }

    /**
     *SUV Car
     * @param builder
     */
    public void constructSUVCar(Builder builder){
        builder.setType(Type.SUV);
        builder.setSeats(4);
        builder.setEngine(new Engine(2.5, 0));
        builder.setThransmission(Thransmission.MANUAL);
        builder.setTripComputer(new TripComputer());
        builder.setGPSNavigator(new GPSNavigator());
    }
}

 

客戶端測試

public class TestClient {
    public static void main(String[] args) {
        Director director = new Director();

        CarBuilder builder = new CarBuilder();
        director.constructCityCar(builder);

        Car car = builder.getResult();
        System.out.println("Car Type"+car.getType());

        CarManualBuilder carManualBuilder = new CarManualBuilder();
        director.constructCityCar(carManualBuilder);
        Manual manual = carManualBuilder.getResult();
        System.out.println("manual+ " +manual.print());
    }
}

技术分享图片

 

java-設計模式-生成器

原文:https://www.cnblogs.com/java-quan/p/13662990.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!