問題描述
很久以前,我一直認為,在java中,為包命名反轉你擁有的域是愚蠢和尷尬的.
For a long time ago, I have thought that, in java, reversing the domain you own for package naming is silly and awkward.
您在項目中使用哪個包命名?
Which do you use for package naming in your projects?
推薦答案
一旦你理解了約定為什么存在,它就不應該感到愚蠢或尷尬.
Once you understand why the convention exists, it shouldn't feel silly or awkward in the least.
這個方案做了兩個重要的事情:
This scheme does two important things:
您的所有代碼都包含在其他人不會與之發生沖突的包中.您擁有自己的域名,因此它是孤立的.如果我們沒有這個約定,很多公司都會有一個實用程序".包,包含StringUtil"、MessageUtil"等類等等.如果你試圖使用其他人的代碼,這些很快就會發生沖突.
All of your code is contained in packages that no one else will collide with. You own your domain name, so it's isolated. If we didn't have this convention, many companies would have a "utilities" package, containing classes like "StringUtil", "MessageUtil" etc. These would quickly collide if you tried to use anyone else's code.
逆向"它的性質使得頂層的類目錄布局非常狹窄.如果您展開一個 jar,您將看到com"、org"、net"等目錄,然后在每個目錄下顯示組織/公司名稱.
The "reverse" nature of it makes class-directory layout very narrow at the top level. If you expand a jar, you'll see "com", "org", "net", etc dirs, then under each of those the organization/company name.
(于 2021 年添加)如今,當這種類型的包命名用于第三方庫時,這一點變得更加重要,這些庫在構建過程中被引入,并且如果名稱不是唯一的,則很容易發生沖突.如果每個人都遵守相同的約定,就不會發生意外碰撞.
(added in 2021) This is even more important nowadays when this type of package naming is used for third-party libraries which are pulled in transitively during builds and could easily conflict if the names were not unique. If everyone adheres to the same convention, there will be no accidental collisions.
(2021 年添加)相同的命名約定可用于應用商店中的應用程序 ID,以確保唯一性.
(added in 2021) The same naming convention can be used for application ids on an app store to ensure uniqueness as well.
我們通常不會擴展 jar,但在早期的 java 開發中,這很重要,因為人們使用擴展的 dir 結構來進行小程序.
We usually don't expand jars, but in early java development, this was important because people used expanded dir structures for applets.
但是,現在這很好,因為源代碼目錄結構具有非常自上而下"的結構.感覺.您從最通用的(com、org、net...)到不太通用的(公司名稱)再到更具體的(項目/產品/lib 名稱).
However, this is nice now as source code dir structures have a very "top-down" feel. You go from the most general (com, org, net...) to less general (company name) to more specific (project/product/lib name).
這篇關于你真的在java中使用你的反向域來命名包嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!