javabeans_膨胀的JavaBeans –不要在您的API中添加“ Getters”
javabeans
我已經(jīng)最近在博客的想法的JavaBeans?如何可以擴(kuò)展以減少在Java世界中,這被廣泛接受的公約設(shè)立的膨脹。 該文章在DZone上重新發(fā)布,并在這里獲得了頗具爭(zhēng)議的反饋(例如,大多數(shù)試圖將一些新想法帶入Java世界的想法)。 我想回顧一下我在該文章中提出的想法之一,該想法被較少關(guān)注,即:
Getter和Setter的命名
為什么每次要操作對(duì)象屬性時(shí)都必須使用那些those腫的“ get” /“ is”和“ set”前綴? 此外,屬性的首字母的大小寫也發(fā)生變化。 如果要對(duì)所有用法進(jìn)行區(qū)分大小寫的搜索
屬性,則必須編寫一個(gè)正則表達(dá)式。 我特別難以理解為什么我們應(yīng)該在各處使用吸氣劑。 Getters / setters是提供對(duì)屬性訪問(wèn)的抽象的約定。 即,您通??偸窃趯戇@樣的愚蠢的東西:
好。 讓我們接受的是,這似乎是我們作為Java開發(fā)人員的日常生活,編寫了所有這些文章,而不是使用標(biāo)準(zhǔn)的關(guān)鍵字或注釋。 我說(shuō)的是標(biāo)準(zhǔn),而不是Project Lombok等專有的東西。 接受生活事實(shí)后,讓我們看一下java.io.File以獲得更多詳細(xì)信息。 對(duì)我來(lái)說(shuō),這是一個(gè)很好的例子,其中JavaBean-o-mania?完全錯(cuò)誤。 為什么? 查看此源代碼摘錄:
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沒什么不同,盡管將來(lái)的版本將解決此問(wèn)題。
如何改善事情
并非所有的庫(kù)和API都存在這種缺陷。 Java已經(jīng)走了很長(zhǎng)一段路,并且已經(jīng)由許多對(duì)此主題有不同看法的人編寫。 此外,Java極具向后兼容性,因此我認(rèn)為JDK如果是從頭開始編寫的,那么它仍然不會(huì)遭受“ JavaBean-o-mania?”的嚴(yán)重影響。 因此,這里有一對(duì)夫婦的規(guī)則可以遵循在新的API,把事情有點(diǎn)清理:
不過(guò),請(qǐng)?jiān)俅慰紤]第一個(gè)規(guī)則。 如果要使用Spring配置bean,則別無(wú)選擇。 但是,如果您不需要Spring,則上述內(nèi)容將具有以下優(yōu)點(diǎn):
- 您的getter,setter和屬性具有完全相同的名稱(以及首字母的大小寫)。 在代碼庫(kù)中進(jìn)行文本搜索要容易得多
- 該getter看起來(lái)像Scala這樣的語(yǔ)言中的屬性本身一樣,由于使用了語(yǔ)言語(yǔ)法糖:“ myBean.myProperty()”和“ myBean.myProperty”,這些屬性等效于表達(dá)式。
- Getter和setter在字典順序上緊挨著(例如,在IDE的“大綱”視圖中)。 這是有道理的,因?yàn)樨?cái)產(chǎn)本身比不采取“獲取”和“設(shè)置”行動(dòng)更為有趣。
- 您無(wú)需擔(dān)心選擇“獲取”還是“是”。 此外,還有一些屬性,無(wú)論如何,“ get” /“ is”無(wú)論如何都是不合適的,例如,只要涉及“ has”->“ getHasChildren()”或“ isHasChildren()”? 嗯,將其命名為“ hasChildren()”! “ setHasChildren(true)”嗎? 不,“ hasChildren(true)”!
- 您可以遵循簡(jiǎn)單的命名規(guī)則:使用命令式動(dòng)詞來(lái)執(zhí)行動(dòng)作。 使用第三人稱形式的名詞,形容詞或動(dòng)詞訪問(wèn)對(duì)象/屬性。 該規(guī)則已經(jīng)證明標(biāo)準(zhǔn)約定存在缺陷。 “ get”是命令形式,而“ is”是第三人稱形式。
或者,返回先前的值,例如:
public int myProperty(int myProperty) {try {return this.myProperty;}finally {this.myProperty = myProperty;}}下定決心并選擇以上任一選項(xiàng),以確保整個(gè)API保持一致。 在大多數(shù)情況下,方法鏈接沒有實(shí)際結(jié)果值有用。
無(wú)論如何,將“ void”作為返回類型浪費(fèi)了API范圍。 具體來(lái)說(shuō),考慮Java 8的lambda語(yǔ)法用于帶/不帶返回值的方法(取自Brian Goetz的lambda表示狀態(tài) ):
// 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上線(對(duì)于那些維護(hù)公共API的人)之后,現(xiàn)在考慮一下這可能是您的API在競(jìng)爭(zhēng)中的決定性優(yōu)勢(shì)。
- 清單
- 地圖
- 參考資料
- ThreadLocals
- 期貨
- 等等…
在所有這些情況下,“獲取”和“設(shè)置”都是操作,而不是屬性訪問(wèn)。 這就是為什么您應(yīng)該使用諸如“ get”,“ set”,“ put”等動(dòng)詞之類的原因。
摘要
設(shè)計(jì)API時(shí)要有創(chuàng)造力。 不要嚴(yán)格遵循JavaBeans?和Spring對(duì)整個(gè)行業(yè)施加的無(wú)聊規(guī)則。 訪問(wèn)對(duì)象/屬性時(shí),最新的JDK API以及Google / Apache著名的API很少使用“獲取”和“設(shè)置”。 Java是一種靜態(tài)的類型安全語(yǔ)言。 表達(dá)式語(yǔ)言和注入配置是我們?nèi)粘9ぷ髦械睦狻?因此,我們應(yīng)該針對(duì)我們處理最多的用例優(yōu)化API。 更好的是,如果Spring將他們的思維方式調(diào)整為漂亮,精簡(jiǎn),漂亮和有趣的API,而不是強(qiáng)迫Java世界使用諸如getter和setter之類的無(wú)聊東西來(lái)夸大他們的API!
參考: Bloated JavaBeans –不要通過(guò)JAVA,SQL和JOOQ博客上的JCG合作伙伴 Lukas Eder 向您的API添加Getter 。
翻譯自: https://www.javacodegeeks.com/2013/02/bloated-javabeans-dont-add-getters-to-your-api.html
javabeans
總結(jié)
以上是生活随笔為你收集整理的javabeans_膨胀的JavaBeans –不要在您的API中添加“ Getters”的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电脑怎么把显卡改成n卡(显卡n卡设置)
- 下一篇: (128ddos.top)