2- builder mode

Builder mode

The builder pattern is a step by step to create a complex object, which allows the user can build them only by specifying the type and content of complex objects, users do not need to know the specific details of the interior of the building.

With the commander of the builder pattern

Product categories

//产品类
public class Car {
    private String wheel;//轮子
    private String engine;//引擎
    private String frame;//车架

    public String getWheel() { return wheel; }
    public void setWheel(String wheel) { this.wheel = wheel; }
    public String getEngine() { return engine; }
    public void setEngine(String engine) { this.engine = engine; }
    public String getFrame() { return frame; }
    public void setFrame(String frame) { this.frame = frame; }

    @Override
    public String toString() {
        return "Car{" +
                "wheel='" + wheel + '\'' +
                ", engine='" + engine + '\'' +
                ", frame='" + frame + '\'' +
                '}';
    }
}

Builders category

//建造者接口
public interface CarBuilder {
    void setWheel();
    void setEngine();
    void setFrame();
    Car build();
}
//建造者实现类
public class AudiCarBuilder implements CarBuilder {
    private Car car=new Car();
    @Override
    public void setWheel() {
        car.setWheel("奥迪轮子");
    }

    @Override
    public void setEngine() {
        car.setEngine("奥迪轮子");
    }

    @Override
    public void setFrame() {
        car.setFrame("奥迪车架");
    }

    @Override
    public Car build() {
        return car;
    }

}

Commander class

//指挥者
public class Director {
    private CarBuilder builder=null;

    public Director(CarBuilder builder) {
        this.builder = builder;
    }
    public Car build(){
        builder.setWheel();
        builder.setEngine();
        builder.setFrame();
        return builder.build();
    }
}

Test category

//测试类
class Test {
    public static void main(String[] args) {
        CarBuilder builder=new AudiCarBuilder();
        Director director=new Director(builder);
        Car car=director.build();
        System.out.println(car);
    }
}

Internal class builder mode

The builder based on the product category as static inner classes, and sets itself so that the method returns to the chain building process ease of use.

Product class and its internal builders category

public class Bike {
    private String wheel;//轮子
    private String frame;//车架

    public String getWheel() { return wheel; }
    public void setWheel(String wheel) { this.wheel = wheel; }
    public String getFrame() { return frame; }
    public void setFrame(String frame) { this.frame = frame; }

    @Override
    public String toString() {
        return "Bike{" +"wheel='" + wheel + '\'' 
            +", frame='" + frame + '\'' +'}';
    }
    public static class BikeBuilder{
        private String wheel;//轮子
        private String frame;//车架
        BikeBuilder setWheel(String wheel){
            this.wheel=wheel;
            return this;
        }
        BikeBuilder setFrame(String frame){
            this.frame=frame;
            return this;
        }
        Bike build(){
            Bike bike=new Bike();
            bike.setFrame(frame);
            bike.setWheel(wheel);
            return bike;
        }
    }
}

Test category

class Test {
    public static void main(String[] args) {
        Bike bike= new Bike.BikeBuilder().setFrame("自行车车架A")
                .setWheel("山地胎")
                .build();
    }
}
Published 27 original articles · won praise 1 · views 907

Guess you like

Origin blog.csdn.net/hu853996234/article/details/103192341