Featured image of post Java设计模式-建造者模式Builder

Java设计模式-建造者模式Builder

相关文章

【合集】Java设计模式

建造者模式

自行车产品

1
2
3
4
5
@Data
public static class Bike {
    private String frame;
    private String seat;
}

自行车抽象构建器

1
2
3
4
5
6
public static abstract class Builder {
    protected Bike bike = new Bike();
    public abstract void buildFrame();
    public abstract void buildSeat();
    public abstract Bike createBike();
}

具体构建器-摩拜单车

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static class MobileBuilder extends Builder {
    @Override
    public void buildFrame() {
        bike.setFrame("碳纤维车架");
    }
    @Override
    public void buildSeat() {
        bike.setSeat("真皮车座");
    }
    @Override
    public Bike createBike() {
        return bike;
    }
}

具体构建器-OFO单车

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 public static class OfoBuilder extends Builder {
    @Override
    public void buildFrame() {
        bike.setFrame("铝合金车架");
    }
    @Override
    public void buildSeat() {
        bike.setSeat("橡胶车座");
    }
    @Override
    public Bike createBike() {
        return bike;
    }
}

指挥者类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Setter
@AllArgsConstructor
public static class Director {
    private Builder builder;

    public Bike construct() {
        builder.buildFrame();
        builder.buildSeat();
        return builder.createBike();
    }
}

客户端调用

1
2
3
4
5
6
7
8
9
// 创建摩拜单车
MobileBuilder mobileBuilder = new MobileBuilder();
Director mobileDirector = new Director(mobileBuilder);
Bike mobile = mobileDirector.construct();

// 创建OFO单车
MobileBuilder ofoBuilder = new OfoBuilder();
Director ofoDirector = new Director(ofoBuilder);
Bike ofo = ofoDirector.construct();

建造者模式-简化

简化系统结构,指挥者类 和 抽象建造者类 合并

自行车产品

1
2
3
4
5
@Data
public static class Bike {
    private String frame;
    private String seat;
}

自行车抽象构建器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public static abstract class Builder {
    protected Bike bike = new Bike();
    public abstract void buildFrame();
    public abstract void buildSeat();
    public abstract Bike createBike();
    public Bike construct() {
        this.buildFrame();
        this.buildSeat();
        return this.createBike();
    }
}

具体构建器-摩拜单车

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static class MobileBuilder extends Builder {
    @Override
    public void buildFrame() {
        bike.setFrame("碳纤维车架");
    }
    @Override
    public void buildSeat() {
        bike.setSeat("真皮车座");
    }
    @Override
    public Bike createBike() {
        return bike;
    }
}

具体构建器-OFO单车

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static class OfoBuilder extends Builder {
    @Override
    public void buildFrame() {
        bike.setFrame("铝合金车架");
    }
    @Override
    public void buildSeat() {
        bike.setSeat("橡胶车座");
    }
    @Override
    public Bike createBike() {
        return bike;
    }
}

客户端调用

1
2
3
4
5
6
7
// 创建摩拜单车
MobileBuilder mobileBuilder = new MobileBuilder();
Bike bike = mobileBuilder.construct();

// 创建OFO单车
MobileBuilder ofoBuilder = new OfoBuilder();
Bike ofo = ofoBuilder.construct();

@Builder注解原理

使用

1
2
3
4
5
6
7
8
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Phone {
    private String cpu;
    private String screen;
}
1
2
3
4
5
6
Phone phone = new Phone
      .Builder()
      .cpu("intel")
      .screen("三星")
      .build();
System.out.println(phone);

原理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@ToString
public class Phone {
    private String cpu;
    private String screen;

    private Phone(Builder builder) {
        cpu = builder.cpu;
        screen = builder.screen;
    }
    public String getCpu() {
        return cpu;
    }
    public void setCpu(String cpu) {
        this.cpu = cpu;
    }
    public String getScreen() {
        return screen;
    }
    public void setScreen(String screen) {
        this.screen = screen;
    }

    public static final class Builder {
        private String cpu;
        private String screen;

        public Builder() {
        }

        public Builder cpu(String val) {
            cpu = val;
            return this;
        }

        public Builder screen(String val) {
            screen = val;
            return this;
        }

        public Phone build() {
            return new Phone(this);
        }
    }

}
皖ICP备2024056275号-1
发表了78篇文章 · 总计148.99k字
本站已稳定运行