1. 創(chuàng)建和銷毀對(duì)象
? ## *1.考慮用靜態(tài)工廠方法代替構(gòu)造器*
? #### 優(yōu)勢(shì)
? ? 1.有名稱
? ? 2.靜態(tài)工廠方法不必每次調(diào)用它們的時(shí)候都創(chuàng)建一個(gè)新的對(duì)象
? ? 3.可以返回原返回類型的任何子類型的對(duì)象 ?
? ? 4.在創(chuàng)建參數(shù)化類型實(shí)例的時(shí)候,它們使代碼變得更加簡(jiǎn)潔归薛。
? #### 區(qū)分“靜態(tài)工廠方法”和“公有構(gòu)造器”辟拷,理解各自的長(zhǎng)處孵坚。
? ---
? ## *2.遇到多個(gè)構(gòu)造器參數(shù)時(shí)要考慮用構(gòu)建器*
? ? ```java
? ? ? ? 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{
? ? ????private final int servingSize;
? ? ????private final int servings;
? ? ????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);
? ? ????}
? ? ??}
? ? ??privcate NutritionFacts(Builder builder){
? ? ????servingSize = builder.servingSize;
? ? ????servings = builder.servings;
? ? ????calories = builder.calories;
? ? ????fat = builder.fat;
? ? ????sodium = builder.sodium;
? ? ????carbohydrate = builder.carbohydrate;
? ? ??}
? ? }
NutritionFacts cocaCola = new NutritionFacts.Builder(240,8).calories(100).sodium(35).carbohydrate(27).build();
? ? ```
總而言之:如果類的構(gòu)造器或靜態(tài)工廠具有多個(gè)參數(shù)贫导,設(shè)計(jì)這種類的時(shí)候易阳,Builder模式就是一個(gè)不錯(cuò)的選擇蔚舀。
3.用私有構(gòu)造器或者枚舉類型強(qiáng)化Singleton屬性
? 單元素的枚舉類型已經(jīng)成為實(shí)現(xiàn)Singleton的最佳方法饵沧。
4.通過私有構(gòu)造器強(qiáng)化不可實(shí)例化的能力
? ? public class UtilityClass {
? ? ? private UtilityClass(){
? ? ? ? throw new AssertionError(); // 拋出AssertionError可以避免內(nèi)部調(diào)用構(gòu)造器
? ? ? }
? ? }
*5.避免創(chuàng)建不必要的對(duì)象*
? String s = new String("aaa"); //不要這樣
? String s = "aaa"; //應(yīng)該這樣
? ```
? //不要這么做
? public class Person{
? ? private final Data birthData;
? ? public boolean isBabyBoomer(){
? ? ? Calendar gmtCal=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
? ? ? gmtCal.set(1946, Calendar.JANUARY,1,0,0,0);
? ? ? return ...
? ? }
? class Person{
? ? private final Data birthDate;
? ? private static final Date BOOM_START;
? ? private static final Date BOOM_END;
? ? // 使用靜態(tài)代碼塊
? ? static{
? ? ? Calendar gmtCal=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
? ? ? gmtCal.set(1946, Calendar.JANUARY,1,0,0,0);
? ? ? BOOM_START = gmtCal.getTime();
? ? ? BOOM_END = ...
? ? }
? ? //改進(jìn)后只需要?jiǎng)?chuàng)建一個(gè)實(shí)例
? ? public boolean isBabyBoomer(){
? ? ? return birthData.compareTo(BOOM_START) >= 0 && birthData.compareTo(BOOM_END) <0;
? ? }
? }?
}
}