这可能适用于建造者模式
public class Stamp {
public static class Builder {
// default values
private String code = "default code"
private int year = 1900;
// etc.
public Builder withCode(String code) {
this.code = code;
return this;
}
public Builder withYear(int year) {
this.year = year;
return this;
}
// etc.
public Stamp build() {
return new Stamp(code, year, country, value, numberOfCopies);
}
}
public Stamp(String code, int year, String country, double value,
int numberOfCopies){...}
}
然后,施工过程变为
Stamp s = new Stamp.Builder()
.withCode("some_code")
.withYear(1991)
.build();
这样,参数不再依赖于顺序 – 您可以等效地说
Stamp s = new Stamp.Builder()
.withYear(1991)
.withCode("some_code")
.build();