膨胀的JavaBeans –不要在您的API中添加“ Getters”
我已經最近在博客的想法的JavaBeans?如何可以擴展以減少在Java世界中,這被廣泛接受的公約設立的膨脹。 該文章在DZone上重新發布,并在這里獲得了頗具爭議的反饋(例如,大多數試圖將一些新想法帶入Java世界的想法)。 我想回顧一下我在該文章中遇到的想法之一,該想法得到了較少的關注,即:
Getter和Setter的命名
為什么每次要操作對象屬性時都必須使用那些those腫的“ get” /“ is”和“ set”前綴? 此外,屬性的首字母的大小寫也發生變化。 如果要對所有用法進行區分大小寫的搜索
屬性,則必須編寫一個正則表達式。 我特別難以理解為什么我們應該在各處使用吸氣劑。 Getters / setters是提供對屬性訪問的抽象的約定。 也就是說,您通常總是會一直這樣寫一些愚蠢的事情:
好。 讓我們接受的是,這似乎是我們Java開發人員的日常生活,編寫所有這些文章而不是使用標準關鍵字或注釋。 我說的是標準,而不是Project Lombok等專有內容。 接受生活事實后,讓我們看一下java.io.File以獲得更多詳細信息。 對我來說,這是一個很好的例子,其中JavaBean-o-mania?完全錯誤。 為什么? 查看此源代碼摘錄:
public class File {// This is the only relevant internal property. It would be 'final'// if it wasn't set by serialisation magic in readObject()private String path;// Here are some arbitrary actions that you can perform on this file.// Usually, verbs are used as method names for actions. Good:public boolean delete();public void deleteOnExit();public boolean mkdir();public boolean renameTo(File dest);// Now the fun starts!// Here is the obvious 'getter' as understood by JavaBeans?public String getPath();// Here are some additional 'getters' that perform some transformation// on the underlying property, before returning itpublic String getName();public String getParent();public File getParentFile();public String getPath();// But some of these 'transformation-getters' use 'to', rather than// 'get'. Why 'toPath()' but not 'toParentFile()'? How to distinguish// 'toPath()' and 'getPath()'?public Path toPath();public URI toURI();// Here are some 'getters' that aren't really getters, but retrieve// their information from the underlying filepublic long getFreeSpace();public long getTotalSpace();public long getUsableSpace();// But some of the methods qualifying as 'not-really-getters' do not// feature the 'get' action keyword, duh...public long lastModified();public long length();// Now, here's something. 'Setters' that don't set properties, but// modify the underlying file. A.k.a. 'not-really-setters'public boolean setLastModified(long time);public boolean setReadable(boolean readable);public boolean setWritable(boolean writable);// Note, of course, that it gets more confusing when you look at what// seem to be the 'not-really-getters' for the abovepublic long lastModified();public boolean canRead();public boolean canWrite(); }困惑? 是。 但是,我們所有人最終都以這種方式做事,一次又一次。 jOOQ沒什么不同,盡管將來的版本將解決此問題。
如何改善事情
并非所有的庫和API都存在這種缺陷。 Java已經走了很長一段路,并且已經由許多對此主題有不同看法的人編寫。 此外,Java極具向后兼容性,因此我認為JDK如果是從頭開始編寫的,那么仍然不會遭受“ JavaBean-o-mania?”的嚴重影響。 因此,這里有一對夫婦的規則可以遵循在新的API,把事情有點清理:
不過,請再次考慮第一個規則。 如果要使用Spring配置bean,則別無選擇。 但是,如果您不需要Spring,則上述內容將具有以下優點:
- 您的getter,setter和屬性具有完全相同的名稱(以及首字母的大小寫)。 在代碼庫中進行文本搜索要容易得多
- 在類似Scala的語言中,getter看起來就像屬性本身一樣,由于語言語法糖:“ myBean.myProperty()”和“ myBean.myProperty”,這些屬性等效于表達式。
- Getter和setter在字典順序上彼此相鄰(例如,在IDE的“大綱”視圖中)。 這是有道理的,因為財產本身比不采取“獲取”和“設置”行動更為有趣。
- 您不必擔心選擇“獲取”還是“是”。 此外,還有一些屬性,無論如何,“ get” /“ is”無論如何都是不合適的,例如,只要涉及“ has”->“ getHasChildren()”或“ isHasChildren()”? 嗯,將其命名為“ hasChildren()”! “ setHasChildren(true)”嗎? 不,“ hasChildren(true)”!
- 您可以遵循簡單的命名規則:使用命令式動詞來執行動作。 使用第三人稱形式的名詞,形容詞或動詞訪問對象/屬性。 該規則已經證明標準約定存在缺陷。 “獲取”是命令形式,而“是”是第三人稱形式。
或者,返回先前的值,例如:
public int myProperty(int myProperty) {try {return this.myProperty;}finally {this.myProperty = myProperty;}}下定決心并選擇以上任一選項,以確保整個API保持一致。 在大多數情況下,方法鏈接沒有實際結果值有用。
無論如何,將“ void”作為返回類型浪費了API范圍。 具體來說,考慮Java 8的lambda語法用于有/無返回值的方法(取自Brian Goetz的lambda表示狀態 ):
// Aaaah, Callables without curly braces nor semi-colons blocks.filter(b -> b.getColor() == BLUE);// Yuck! Blocks with curly braces and an extra semi-colon! blocks.forEach(b -> { b.setColor(RED); });// In other words, following the above rules, you probably // prefer to write: blocks.filter(b -> b.color() == BLUE).forEach(b -> b.color(RED));Java 8上線(對于那些維護公共API的人)之后,現在考慮一下這可能是您的API在競爭中的決定性優勢。
- 清單
- 地圖
- 參考文獻
- ThreadLocals
- 期貨
- 等等…
在所有這些情況下,“獲取”和“設置”都是操作,而不是屬性訪問。 這就是為什么您應該使用動詞,例如“ get”,“ set”,“ put”和許多其他動詞的原因。
摘要
設計API時要有創造力。 不要嚴格遵循JavaBeans?和Spring對整個行業施加的無聊規則。 訪問對象/屬性時,最新的JDK API和Google / Apache著名的API很少使用“ get”和“ set”。 Java是一種靜態的類型安全語言。 表達式語言和注入配置是我們日常工作中的例外。 因此,我們應該針對我們處理最多的用例優化API。 更好的是,如果Spring將他們的思維方式調整為漂亮,精簡,漂亮和有趣的API,而不是強迫Java世界使用諸如getter和setter之類的無聊東西來夸大他們的API!
參考: Bloated JavaBeans –不要通過JAVA,SQL和JOOQ博客上的JCG合作伙伴 Lukas Eder 向您的API添加Getter 。
翻譯自: https://www.javacodegeeks.com/2013/02/bloated-javabeans-dont-add-getters-to-your-api.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的膨胀的JavaBeans –不要在您的API中添加“ Getters”的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 您的日志就是您的数据:logstash
- 下一篇: 同步多线程集成测试