1. Minimize the accessibility of classes and members
- 可以宣告為越私有就越私有
- 只屬於某個class會用到的class就用inner class
- member 盡量為private
- subclass不可以比superclass更private
- interface一定是public
- instance fields 一定不可以是 public
- static final fields也不可以是public, 因為object refer到的object會被改到
2. In public classes, use accessor methods, not public fields
3. Minimize mutability
- 使一個class immutable
no modify method, can't be extended, all fields final, instance fields是object reference 要小心指到的物件不會被改
- 沒有thread, synchronization的問題, 可以被share
- 可以拿來被當做常數使用
- 在static factory可以cache, 因為同一個value的物件一定是唯一的
- 會有performance的問題
- mutable companion class, 就像String and StringBuilder
- 用 private constructor 而不要用final
- Serializable要注意 readObject / readResolve
- 如果不能完全做到immutable, 也要盡量減少可以被修改的fields
4. Favor composition over inheritance
- 只有在同一個package or 有特別說明使用來被繼承的class, 才去繼承
- 缺點
1. 違反 encapsulation, 因為要說清楚人家才好繼承
2. 容易因為superclass修改就傷害了繼承的class, 甚至可能不能compile
- 只有確定是 is-a 關西, 才考慮用繼承
5. design and document for inheritance or else prohibit it
- 必須說明有哪幾個methods有用到overridable methods
- 說明這個method應該如何被override的說明
- 自己寫個subclass測試看看
- Constructor不可以使用overridable method
- Cloneable & Serializable 不繼承
6. Prefer interfaces to abstract classes
- 現有的class較容易改寫成某個interface
- interface可以多個
- 最好是 interface + abstract class => skeletal implementation
7. Use interfaces only to define types
- 不要用來定義 Constants
8. Prefer class hierarchies to tagged classes
- 同一個class不要同時可以變成兩種物件
9. Use function objects to represent strategies
10. Favor static member classes over nonstatic
- static member class
- non-static member class
- anonymous inner class => 一定要繼承或是implement某個interface
- local classes
About Me
星期一, 6月 30, 2008
Effective Java - Chapter 4 Classes and Interfaces
星期日, 6月 22, 2008
Effective Java筆記 - Chapter 3
宣告 @Override 確定有正確Override
1. equals
- Reflexive
- Symmetric
- Transitive
- Consistent
- not equals to null
- 使用composition, 不用extends
- 先測試 instance of
- 不要依靠unreliable resources
- 測試最顯著的fields
2. Always override hashCode when overriding equals
- same object return the same hash code
- equal object must have equal hash codes
- two different object return different hash code
3. Aways override toString
4. Override clone judiciously
- implement Clonable
- 如果只有 primitive fields, 可直接呼叫 super.clone()
- 物件則只會clone reference pointer, 需特別處理
- 不可以有finalize object
- 需留意clone時的performance
- 提供另外的copy constructor
- 盡量不要用
5. Consider implementing Comparable
- 與equals類似的性質
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 關閉應該關閉的物件

