preface
After reading the previous chapters of Effective Java, combined with their actual use, record some useful points
1. Replace the constructor with the static factory method
Consider using the static factory method instead of the constructor
public class Pizza {
private String dough;
private String sauce;
private String topping;
private String cheese;
public Pizza(String dough, String sauce, String topping,String cheese) {
this.dough = dough;
this.sauce = sauce;
this.topping = topping;
this.cheese = cheese;
}
public static Pizza noCheesePizza(String dough, String sauce, String topping){
return new Pizza(dough,sauce,topping,null);
}
public static Pizza cheesePizza(String dough, String sauce, String topping,String cheese){
return new Pizza(dough,sauce,topping,cheese);
}
}
Advantage 1: the name of the static factory method is named by itself, and the constructor must be the same as the class name
If we want to provide multiple constructors with the same signature, we can't do it in Java unless we adjust the order of the parameter list, but it will be very unfriendly to users.
Therefore, you can use the static factory method to take different names to represent different construction methods.
It is applicable to the case that the class contains multiple constructors. We can return the objects we need through accurately named static methods
Advantage 2: the constructor will create an object every time it is called, while the static factory method will not create an object to be given every time it is called.
For singleton mode.
Advantage 3: the static factory method can return any subtype object of the original return type, which gives us greater flexibility in selecting the class of the return object
Applied to polymorphism
Disadvantages:
a. A class cannot be subclassed if it does not contain a public class or a protected constructor
b. They are actually no different from other static methods, so we have agreed on some common names of static factory methods
2. Consider using Builder mode when multiple constructor parameters are encountered
General practice:
For example, there is a constructor for this class:
public Register(int id, int age, String name, String address, String like, int high, int weight)
What to do at this time:
new Register(1, 20, "A", "B", "C", 170, 60);
Such disadvantages are:
1. The parameters will be messy, and it is easy for the caller to mistake the position of the parameters
2. If you don't want to assign a value to a parameter, you still have to assign it unless you write another constructor without the value
Builder mode
public class Register {
private int id;
private int age;
private String name;
private String address;
private String like;
private int high;
private int weight;
public static class Builder {
private int id;
private String name;
private int age;
private String address;
private String like;
private int high;
private int weight;
public Builder(int id, String name) {
this.id = id;
this.name = name;
}
public Builder age(int age){
this.age = age;
return this;
}
public Builder address(String address){
this.address = address;
return this;
}
public Builder like(String like){
this.like = like;
return this;
}
public Builder high(int high){
this.high = high;
return this;
}
public Builder weight(int weight){
this.weight = weight;
return this;
}
public Register build(){
return new Register(this);
}
}
//Privatization
private Register(Builder builder){
id = builder.id;
name = builder.name;
age = builder.age;
address = builder.address;
like = builder.like;
high = builder.high;
weight = builder.weight;
}
}
Usage at this time:
Register re = new Register.Builder(1, "liu").age(20).address("LA").like("Ball").high(180);
3. When using an array or collection, return an array or collection of zero length instead of null
It can avoid programmers calling and reporting NullPointerException without judging null
4. Give priority to the combination
Combinatorial relationship | Inheritance relationship |
Advantages: it does not destroy the encapsulation, and the overall class and local class are loosely coupled and relatively independent of each other | Disadvantages: the encapsulation is broken, the subclass is closely coupled with the parent class, the subclass depends on the implementation of the parent class, and the subclass lacks independence |
Advantages: it has good scalability | Disadvantages: it supports expansion, but often at the cost of increasing the complexity of the system structure |
Advantages: support dynamic combination. At run time, the overall object can select different types of local objects | Disadvantages: dynamic inheritance is not supported. A child class cannot select a different parent class at run time |
Advantages: the overall class can package the local class, encapsulate the interface of the local class, and provide a new interface | Disadvantages: dynamic inheritance is not supported. A child class cannot select a different parent class at run time |
Disadvantages: the overall class cannot automatically obtain the same interface as the local class | Advantage: the subclass can automatically inherit the interface of the parent class |
Disadvantages: when creating an object of an overall class, you need to create objects of all local classes | Advantage: when you create an object of a subclass, you do not need to create an object of a parent class |
Summary:
- Inherit only when is-a is satisfied
- If the API of the parent class has defects, do inheritance worry about passing these defects to the child class?
- Inheritance breaks encapsulation
- Preferred combination