重叠构造器模式
——不能很好地扩展:
public class Person {
private String name; // 必填
private int age; // 必填
private String address; // 可选
private String email; // 可选
private String phone; // 可选
private String occupation; // 可选
// 无参构造方法
public Person(){}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, String address) {
this(name, age, adress, null);
}
public Person(String name, int age, String address, String email) {
this(name, age, adress, email, null);
}
public Person(String name, int age, String address, String email, String phone) {
this(name, age, adress, email, phone, null);
}
public Person(String name, int age, String address, String email, String phone, String occupation) {
this.name = name;
this.age = age;
this.address = address;
this.email = email;
this.phone = phone;
this.occupation = occupation;
}
// Getters and setters
}
JavaBeans模式
——允许不一致性,要求可变性:
public class Person {
private String name; // 必填
private int age; // 必填
private String address; // 可选
private String email; // 可选
private String phone; // 可选
private String occupation; // 可选
// 无参构造方法
public Person(){}
// setters
public void setName (String name) { this.name = name;}
public void setAge(String age) { this.age = age;}
public void setAddress (String address) { this.address = address;}
public void setEmail (String email) { this.email = email;}
public void setPhone (String phone) { this.phone = phone;}
public void setOccupation (String occupation) { this.occupation = occupation;}
}
Person person = new Person();
person.setName("name");
person.setAge("age");
person.setAddress("address");
person.setEmail("email");
person.setPhone("phone");
person.setOccupation ("occupation");
生成器模式:
public class Person {
private String name; // 必填
private int age; // 必填
private String address; // 可选
private String email; // 可选
private String phone; // 可选
private String occupation; // 可选
private Person(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.address = builder.address;
this.email = builder.email;
this.phone = builder.phone;
this.occupation = builder.occupation;
}
// Getters
public static class Builder {
private String name;
private int age;
private String address;
private String email;
private String phone;
private String occupation;
public Builder(String name, int age) {
this.name = name;
this.age = age;
}
public Builder address(String address) {
this.address = address;
return this;
}
public Builder email(String email) {
this.email = email;
return this;
}
public Builder phone(String phone) {
this.phone = phone;
return this;
}
public Builder occupation(String occupation) {
this.occupation = occupation;
return this;
}
public Person build() {
return new Person(this);
}
}
}
使用示例:
Person person = new Person.Builder("John", 25)
.address("123 Main St")
.email("john@example.com")
.build();
在上述示例中,Person
类使用了一个内部的 Builder
类来构建对象。必填的参数通过构造函数传入,而可选的参数则通过链式调用建造者的方法进行设置。最后,通过调用 build()
方法来创建最终的 Person
对象。
使用生成器模式,你可以根据需要选择要设置的可选参数,而不必担心参数顺序或构造函数重载的问题。这种模式提供了更灵活和可扩展的方式来创建对象,并且可以轻松地处理不同参数组合的情况。