日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Effective Java 学习笔记 1

發布時間:2023/12/31 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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就是服務接口,OrcaleSQLServerMySQL等是服務提供者,它們實現相應的服務接口;DriverManager.registerDriver是提供者注冊API,向DriverManager注冊給定驅動程序;DriverManager.getConnection是服務訪問APIDriver就是服務提供者接口。

下面是書中給出的一個服務提供商框架的例子

?

?

  • //?Service?provider?framework?sketch??
  • //?Service?interface??
  • public?interface?Service?{??
  • ...?//?Service-specific?methods?go?here??
  • }??
  • //?Service?provider?interface??
  • public?interface?Provider?{??
  • Service?newService();??
  • }??
  • //?Noninstantiable?class?for?service?registration?and?access??
  • public?class?Services?{??
  • private?Services()?{?}?//?Prevents?instantiation?(Item?4)??
  • //?Maps?service?names?to?services??
  • private?static?final?Map<String,?Provider>?providers?=??
  • new?ConcurrentHashMap<String,?Provider>();??
  • public?static?final?String?DEFAULT_PROVIDER_NAME?=?"<def>";??
  • //?Provider?registration?API??
  • public?static?void?registerDefaultProvider(Provider?p)?{??
  • registerProvider(DEFAULT_PROVIDER_NAME,?p);??
  • }??
  • public?static?void?registerProvider(String?name,?Provider?p){??
  • providers.put(name,?p);??
  • }??
  • //?Service?access?API??
  • public?static?Service?newInstance()?{??
  • return?newInstance(DEFAULT_PROVIDER_NAME);??
  • }??
  • public?static?Service?newInstance(String?name)?{??
  • Provider?p?=?providers.get(name);??
  • if?(p?==?null)??
  • throw?new?IllegalArgumentException(??
  • "No?provider?registered?with?name:?"?+?name);??
  • return?p.newService();??
  • }??
  • }??
  • ?

    ? ? ?A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances. (可以使創建參數化類型實例更佳簡潔)

    ? ? ?比如:創建HashMap時,就需重復輸入類型參數

  • Map<String,?List<String>>?m?=?new?HashMap<String,?List<String>>();?
  • ? ? ?使用靜態工廠方法可以解決這一問題

  • //靜態工廠方法,類型參數只需要在這里寫一遍即可?
  • public?static?<K,?V>?HashMap<K,?V>?newInstance()?{?
  • return?new?HashMap<K,?V>();?
  • }?
  • //創建對象時,就不用重復類型參數了。?
  • Map<String,?List<String>>?m?=?HashMap.newInstance();?
  • ? ? ?靜態工廠方法也有一些不足之處:

    ? ? ?The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed(最大的缺點是,如果類只提供了靜態工廠方法而沒有提供publicprotected的構造器的話,不能派生子類)

    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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。