您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Builder模式是一种创建型设计模式,它允许你分步骤地构建复杂的对象。这种模式的主要优点在于它可以将对象的构建过程与其表示分离,使得同样的构建过程可以创建不同的表示。以下是Java中Builder模式的一些适用场景:
当一个对象需要多个参数来初始化,并且这些参数中有些是可选的,或者参数的组合方式很多时,使用Builder模式可以简化对象的创建过程。
示例:
public class Car {
private final String make;
private final String model;
private final int year;
private final boolean sunroof;
private final boolean leatherSeats;
private Car(Builder builder) {
this.make = builder.make;
this.model = builder.model;
this.year = builder.year;
this.sunroof = builder.sunroof;
this.leatherSeats = builder.leatherSeats;
}
public static class Builder {
private final String make;
private final String model;
private final int year;
private boolean sunroof;
private boolean leatherSeats;
public Builder(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public Builder sunroof(boolean sunroof) {
this.sunroof = sunroof;
return this;
}
public Builder leatherSeats(boolean leatherSeats) {
this.leatherSeats = leatherSeats;
return this;
}
public Car build() {
return new Car(this);
}
}
}
当一个类的构造函数参数过多时,使用Builder模式可以使代码更加清晰和易读。
示例:
public class User {
private final String name;
private final int age;
private final String email;
private final String address;
private final String phoneNumber;
private User(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.email = builder.email;
this.address = builder.address;
this.phoneNumber = builder.phoneNumber;
}
public static class Builder {
private final String name;
private final int age;
private String email;
private String address;
private String phoneNumber;
public Builder(String name, int age) {
this.name = name;
this.age = age;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Builder phoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
return this;
}
public User build() {
return new User(this);
}
}
}
当对象的参数顺序不重要,或者参数的组合方式很多时,使用Builder模式可以避免构造函数参数顺序错误的问题。
示例:
public class Pizza {
private final String dough;
private final String sauce;
private final List<String> toppings;
private Pizza(Builder builder) {
this.dough = builder.dough;
this.sauce = builder.sauce;
this.toppings = builder.toppings;
}
public static class Builder {
private final String dough;
private final String sauce;
private final List<String> toppings = new ArrayList<>();
public Builder(String dough, String sauce) {
this.dough = dough;
this.sauce = sauce;
}
public Builder addTopping(String topping) {
this.toppings.add(topping);
return this;
}
public Pizza build() {
return new Pizza(this);
}
}
}
当对象的构建过程需要根据不同的条件进行调整时,使用Builder模式可以提供更大的灵活性。
示例:
public class House {
private final String foundation;
private final String walls;
private final String roof;
private final boolean hasGarden;
private House(Builder builder) {
this.foundation = builder.foundation;
this.walls = builder.walls;
this.roof = builder.roof;
this.hasGarden = builder.hasGarden;
}
public static class Builder {
private final String foundation;
private final String walls;
private final String roof;
private boolean hasGarden;
public Builder(String foundation, String walls, String roof) {
this.foundation = foundation;
this.walls = walls;
this.roof = roof;
}
public Builder hasGarden(boolean hasGarden) {
this.hasGarden = hasGarden;
return this;
}
public House build() {
return new House(this);
}
}
}
Builder模式适用于以下场景:
通过使用Builder模式,可以提高代码的可读性和可维护性,同时减少因参数顺序错误导致的bug。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。