About Me

我的相片
Mr. Pigg
Taipei, Taiwan
檢視我的完整簡介

星期日, 6月 22, 2008

Effiective Java筆記 - Chapter 2

1. Consider static factory method instead of constructors - static factory method 可以使用比較有意義的名字
使用constructor只能使用不同的參數來宣告多個constructor, 使用者很難分辨
- 可避免產生不必要產生的物件, 易於控制物件的產生
- 可以回傳subclass物件, 易於實現Service Provider

缺點
- 不提供constructor無法被繼承
- 長相跟其他static method沒有區分

2. Consider a builder when faced with many constructor parameters
3. Enforce the singleton property with a private constructor or an enum type - 小心serializable

4. Enforce noninstantiability with a private constructor - For a group static methods and fields

5. Avoid creating unnecessary objects - 不要new immutable object, 如String , Boolean
- 重複使用可重複使用的物件
- 避免不必要的autoboxing

6. Eliminate obsolete object references - Class 管理自己的記憶體即可能產生memory leak
- 盡量縮小每一個變數的scope
- 善用WeakHashMap

7. Avoid finalizers
- 使用 try finally 關閉應該關閉的物件

0 意見: