1. Give priority to static factory methods instead of constructors
When a class needs to have multiple constructors, consider using static factory methods instead of these constructors.
The benefits of doing this are:
First: The static factory method can have a clearer description of the object to be created through the method signature, while the constructor method cannot
Second: static factory methods can avoid creating the same object repeatedly (Boolean example can illustrate)
Third: static factory methods can return any subclass of the desired return type (this is very useful in frameworks, especially APIs)
Fourth: Code can be simplified when creating parameterized instances (HashMap example can illustrate)
Example:
1. Boolean example:
publice static Boolean valueOf(boolean b){
return b ? Boolean.TRUE : Boolean.FALSE;
}
2. HashMap example:
If you want to construct such an instance:
Map<String, List<String>> map = new HashMap<String, List<String>> ();
It's cumbersome to implement through constructors, but relatively simple through static factory methods.
publice static <K, V>Map<K, V> newInstance(){
return new HashMap<K, V>();
}
Map<String, List<String>> map = HashMap.newInstance();
Second, use the Builder pattern to handle the creation of multi-construction parameter objects
1. When an object has multiple construction parameters and most of them are optional, you should consider using the Builder pattern.
2. After a class is created, if you want to extend member variables later, you should consider using the Builder pattern.
Why not use cascading constructors and static factory methods?
answer:
Too many cascading constructors are not user-friendly, and will bring certain maintenance costs.
Static factory methods are more suitable for constructing objects with few parameters.
Example :
public classCat{
public static void main(String[] args) {
Cat cat = new CatBuilder().name("小花").sex('母').color("❀").weight(2.5).build();
System.out.println(cat.getColor());
}
private String color;
private Integer age;
private Character sex;
private String name;
private Double weight;
public static classCatBuilder{
private String color;
private Integer age;
private Character sex;
private String name;
private Double weight;
public CatBuilder color(String color) {
this.color = color;
return this;
}
public CatBuilder age(Integer age) throws Exception {
if (0 > age) throw new Exception("年龄输入错误");
this.age = age;
return this;
}
public CatBuilder sex(Character sex) {
this.sex = sex;
return this;
}
public CatBuilder name(String name) {
this.name = name;
return this;
}
public CatBuilder weight(Double weight) {
this.weight = weight;
return this;
}
public Cat build() {
Cat cat = new Cat();
cat.color = this.color;
cat.age = this.age;
cat.sex = this.sex;
cat.name = this.name;
cat.weight = this.weight;
return cat;
}
}
public String getColor() {
return color;
}
public Integer getAge() {
return age;
}
public Character getSex() {
return sex;
}
public String getName() {
return name;
}
public Double getWeight() {
return weight;
}
@Override
public String toString() {
return "Cat{" +
"color='" + color + '\'' +
", age=" + age +
", sex=" + sex +
", name='" + name + '\'' +
", weight=" + weight +
'}';
}
}