區(qū)分好的模塊和不好的模塊最重要的因素是看這個(gè)模塊對(duì)于其他模塊而言是否隱藏內(nèi)部數(shù)據(jù)和其他細(xì)節(jié)。好的模塊會(huì)把所有細(xì)節(jié)隱藏起來,把API和實(shí)現(xiàn)隔離開來,模塊之間用API通信。這就是information hiding或者封裝(encapsulation)。是軟件設(shè)計(jì)基本原則之一。
information hiding最大的意義在于it decouples(解耦) the modules that comprise a system. 這樣模塊就能獨(dú)立開發(fā)、測試。提高了可重用性。
Java中很多facility協(xié)助了信息隱藏,比如訪問控制access control,決定了類,接口,成員的accessibility。
The rule of thumb: 盡可能使每個(gè)類或者成員不被外界訪問。也就是給最小的訪問級(jí)別。
頂層類和接口
Top level(non-nested)的classes and interfaces,只有兩種可能的訪問級(jí)別:
-
package-priavte(筆者注:也就是default)
The member is accessible from any class in the package
where it is declared - public
成員(fields, methods, nested classes, and nested interfaces)
-
private—The member is accessible only from the top-level class where it is
declared. -
package-private—The member is accessible from any class in the package
where it is declared. Technically known as default access, this is the access level
you get if no access modifier is specified. -
protected—The member is accessible from subclasses of the class where it is
declared (subject to a few restrictions [JLS, 6.6.2]) and from any class in the
package where it is declared. - public—The member is accessible from anywhere.
從package-private變成protected時(shí),accessibility會(huì)大大增強(qiáng)。protected members應(yīng)該盡量少用。
子類中覆蓋的方法的訪問級(jí)別不能低于父類的那個(gè)。特別的,對(duì)于接口來說,接口中所有的方法都隱含著公有訪問級(jí)別;所以如果一個(gè)類實(shí)現(xiàn)了接口,接口中所有方法在這個(gè)類中也必須被聲明為公有的。
instance field永遠(yuǎn)不能是public的。
Classes
with public mutable fields are not thread-safe.
這一點(diǎn)也就解釋了之前我討論的為什么android中的context不能寫成 public static Context context;(當(dāng)然對(duì)于android來說,private也不行,因?yàn)閏ontext不能是static)。這一點(diǎn)我還是不太明白,主要不明白instance field是啥,與之對(duì)應(yīng)的static field是啥。同樣的建議適用于靜態(tài)域。
總之,防止任何散亂的類、接口、成員變成API的一部分。除了Public static final的特殊情形,public class都不應(yīng)該含有public fields. 還要確保public static final域的對(duì)象都是不可變的,比如,不能定義一個(gè)public static final Things[] VALUES = {...};因?yàn)榉?數(shù)組是可變的。