Effective Java 学习笔记 1
?Item 1: Consider static factory methods instead of constructors? (多考慮使用靜態工廠方法而不是構造方法)
?????? 使用靜態工廠方法有以下幾點好處:
One advantage of static factory methods is that, unlike constructors, they have names.(一個好處是與構造方法不同,靜態工廠方法有名字)。 使用靜態工廠方法可以說明客戶程序員理解程序。因為靜態工廠方法可以去一些比較有意義的名字使代碼可讀性更高。比如
BigInteger(int, int, Random) 這個構造方法是返回一個很有可能是質數的BigInteger, 從這個構造方法 根本不能很明確的得到這一信息。這時候可以采用一個靜態工廠方法來說明理解,如BigInteger.probablePrime。 靜態方法的名字 probablePrime 很明確地告訴了客戶程序員這個方法是干什么的。
A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked. (第二個好處是,與構造方法不同,靜態工廠方法不需要再每次被調用的時候創建一個新的對象,這個類似于設計模式中的Flyweight 享元模式)
靜態工廠方法可以在多次調用的時候返回同一個對象,可以讓類嚴格地管理它的實例(什么時候應該銷毀,什么時候應該創建等等)。這種類可以叫做 instance-controlled.? Instance control可以讓類實現單例(Item 3),不可實例化(Item 4)等等的特殊需求。它同樣允許,不可變類(Item 15)的實現 以保證沒有兩個相同實例 存在。 那么也就意味著 a.equals(b) 當且僅當 a==b的時候成立。所以客戶程序員可以使用==代替 equals方法,這樣可以提高程序的運行效率。
A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.(使用靜態工廠方法,與構造方法不同,可以返回其返回類型的任意子類型)
這一優點保證了程序的靈活性,這也是service provider frameworks(服務提供商框架)的基礎之一。
服務提供商框架一共有四個部分組成:
1.??????? Service interface:服務接口,通過抽象統一聲明,由服務提供者實現。
2.??????? provider registration API:服務提供者注冊API,用于系統注冊服務提供者,使得客戶端可以訪問它實現的服務
3.??????? Service access API:服務訪問API,用戶客戶端獲取相應的服務。
4.??????? Service provider interface(Optional): 服務提供者接口,這些服務提供者負責創建其服務實現的實例。
對于JDBC來說,Connection就是服務接口,Orcale、SQLServer、MySQL等是服務提供者,它們實現相應的服務接口;DriverManager.registerDriver是提供者注冊API,向DriverManager注冊給定驅動程序;DriverManager.getConnection是服務訪問API;Driver就是服務提供者接口。
下面是書中給出的一個服務提供商框架的例子
?
?
?
? ? ?A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances. (可以使創建參數化類型實例更佳簡潔)
? ? ?比如:創建HashMap時,就需重復輸入類型參數
? ? ?使用靜態工廠方法可以解決這一問題
? ? ?靜態工廠方法也有一些不足之處:
? ? ?The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed(最大的缺點是,如果類只提供了靜態工廠方法而沒有提供public和protected的構造器的話,不能派生子類)
A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods. (第二個缺點就是使用靜態工廠方法,客戶程序員從文檔中很難將其與其他靜態方法分辨)第二個缺點涉及到javadoc,因為javadoc不會對靜態工廠方法進行特殊處理,所以客戶程序員不能像查詢構造方法一樣 很快的找到靜態工廠方法。
以下是一些常用的靜態工廠方法的命名方法:
?
?
? valueOf—Returns an instance that has, loosely speaking, the same value as its?
parameters. Such static factories are effectively type-conversion methods.? of—A concise alternative to valueOf, popularized by EnumSet (Item 32).
? getInstance—Returns an instance that is described by the parameters but?
cannot be said to have the same value. In the case of a singleton, getInstance?takes no parameters and returns the sole instance.? newInstance—Like getInstance, except that newInstance guarantees that?
each instance returned is distinct from all others.? getType—Like getInstance, but used when the factory method is in a different?
class. Type indicates the type of object returned by the factory method.? newType—Like newInstance, but used when the factory method is in a different?
class. Type indicates the type of object returned by the factory method.總體來說,靜態工廠方法和構造器都有其自己的作用,但是一般說來還是優先使用靜態工廠方法。
?
?
轉載于:https://blog.51cto.com/befenghan/1132642
總結
以上是生活随笔為你收集整理的Effective Java 学习笔记 1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为往开源的鸿蒙里投毒 华为为什么要开发
- 下一篇: 360公布营收数据:抢占企业级大模型市场