`

未完 java设计: naming convention | 命名规范

阅读更多
   
应该遵循的规范:
类/接口/属性名,使用名词或形容词;
方法名使用动词。
Java Naming Conventions:
http://www.oracle.com/technetwork/java/codeconventions-135099.html


Ixxx、Service、servieimpl、dao、daoimpl,真的有必要吗?
Naming the Java Implementation Classes:
http://isagoksu.com/2009/development/java/naming-the-java-implementation-classes/
http://stackoverflow.com/questions/2814805/java-interfaces-implementation-naming-convention


对象的 State & Behavior:
state is stored in fields and behavior is shown via methods
对象的属性及其对应的值构成对象的状态:An object's state is defined by the attributes of the object and by the values these have.通俗的讲可以将state理解成对象的属性(字段)。
很多的设计模式中,都遵循“separate state and behavior”的原则。state即字段,behavior即方法,既然要分离出对象的行为(方法)来,而类/接口的名称约定一般为名词,所以,分离出的行为接口的命名,一般也就遵循“动词变名词”的原则,如发送email有个方法叫send,分离出发送行为后,可以将其命名为EmailSender;某类Xxx有个print方法,分离出打印的行为后,可以将其命名为XxxPrinter等;分离出账单类Bill的计算账单方法calculate()后,可以将分离出的"计算账单"这一行为的接口命名为BillCalculator等,诸如此类。



包的划分原则:Package by feature or layer?
http://stackoverflow.com/questions/11733267/is-package-by-feature-approach-good
包的划分,可以按 feature(功能)来,也可能按 layer(action/controller/service/dao/model 等)来。
但有一点需要注意的是,在同一层级上,不要混合使用 feature-based 和 layer-based 划分策略,不然容易造成混乱。
一个建议的划分方法是,先按 feature 划分,再在各个 feature 下按 layer 来划分。如:
引用
com
--xxx
----email
------composer
--------service
--------dao
--------model
--------and so on
------sender
--------service
--------dao
--------model
--------and so on

问题,分离 Api(interface) 和 Impl 的包结构,属于 feature-based 还是 layer-based?




DB 的 table name 及其对应的 Java Entity name,应该是写作单数形式(Singular)还是复数形式(Plural)?
http://stackoverflow.com/questions/808992/singular-or-plural-database-table-names
引用
Java Entity 的类名,请一律使用单数形式;也包括 enum 的名字,参见 http://stackoverflow.com/questions/15755955/java-enums-singular-or-plural
而对应的 DB 的 table name,建议使用复数形式。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics