考虑用静态工厂方法代替构造器
- 静态工厂方法与构造器第一大优势在于,静态工厂方法有名称,当有多个构造器时,常常因为构造器名字相同而不知道调用那个构造器
- 第二大优势在于静态工厂方法不用每次创建一个对象
- 第三大优势在于可以返回原返回类型的子类型的对象
- 第四大优势在于,创建参数化实例类时,他们使代码变得更简介,而在调用参数化类的构造器时,你必须提供两次类型参数(参数化类,即带泛型的类,用类型来传递参数类型)
//参数化类构造器
Map<String,List<String>> m=new HashMap<String,List<String>>();
//假设该类提供如下方法
public static<K,V> HashMap<K,V> newInstance(){
return new HashMap<K,V>();
}
//是不是以下代码就简洁多了
Map<String,List<String>> m=HashMap.newInstance();
静态工厂方法也有如下缺点
- 类不含有公有的或受保护的(protected)构造器,就不能被子类化
- 静态工厂方法和其他静态方法名字区别不大,但可通过定义命名规范解决
遇到多个构造器参数时要考虑构建器
- 当有多个参数时,重叠构造器模式可行,但是会很难编写,难以阅读
- javaBean也是一种方法(即提供一个无参构造器,用set方法来设置参数),但是javaBean,把阻止了把类变成不可变的可能,需要确保线程安全
- BUilder模式(构建器模式)
public class NutritionFacts {
private final int servingSize;//食用量
private final int servings;//可供食用人数
private final int calories;//卡路里
private final int fat;
private final int sodium;//钠
private final int carbohydrate;
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}
public Builder calories(int val) {
calories = val;
return this;
}
public Builder fat(int val) {
fat = val;
return this;
}
public Builder carbohydrate(int val) {
carbohydrate = val;
return this;
}
public Builder sodium(int val) {
sodium = val;
return this;
}
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
public static void main(String[] args) {
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8)
.calories(100).sodium(35).carbohydrate(27).build();
}
}
用私有构造器和枚举类强化 Singleton(单例)属性
- 私有构造器
- 枚举强化
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() {
System.out.println("Whoa baby, I'm outta here!");
}
// This code would normally appear outside the class!
public static void main(String[] args) {
Elvis elvis = Elvis.INSTANCE;
elvis.leaveTheBuilding();
}
}
###避免创建不必要的对象
- String str=new String("aa")每次执行都会创建对象,最好用String str="sdsd"
- 用static来加载只需加载一次的资源
class Person {
private final Date birthDate;
public Person(Date birthDate) {
// Defensive copy - see Item 39
this.birthDate = new Date(birthDate.getTime());
}
// Other fields, methods
/**
* The starting and ending dates of the baby boom.
*/
private static final Date BOOM_START;
private static final Date BOOM_END;
static {
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_END = gmtCal.getTime();
}
public boolean isBabyBoomer() {
return birthDate.compareTo(BOOM_START) >= 0
&& birthDate.compareTo(BOOM_END) < 0;
}
}
消除过期对象
- 有些过期对象不会被回收器回收,这时候需要你手动置为null