简体   繁体   English

Java体系结构编码约定

[英]Java architecture coding conventions

I have been working at a few different companies now and every one has different rules about how to name classes and packages. 我现在在几家不同的公司工作,每个人都有关于如何命名类和包的不同规则。 They each have different package layouts and workflow between classes. 它们各自具有不同的包布局和类之间的工作流程。 Through these experiences I have picked up an understanding of how to layout out a project; 通过这些经历,我了解了如何布局项目; however, I would like a more concrete definition of how to layout a project. 但是,我想更具体地定义如何布局项目。 This question is more about uml than about naming conventions. 这个问题更多的是关于uml而不是关于命名约定。

What I am curious is what are the official architecture definitions regarding the following (I have seen helpers used as utilities and managers used as helpers etc). 我很好奇的是关于以下内容的官方架构定义是什么(我已经看到帮助器用作实用程序和管理器用作帮助程序等)。

  • "class" helper “阶级”助手
  • "class" utility “阶级”效用
  • "class" factory “班级”工厂
  • "class" manager “班级”经理
  • simple "class" 简单的“阶级”
  • default "class" 默认“类”
  • my "class" 我的课”

To me: 对我来说:

  • A Helper is a facade, or it codes/decodes a particular data format and does not have state between calls. 帮助程序是一个外观,或者它编码/解码特定的数据格式,并且在调用之间没有状态。

  • A Utility is for coding/decoding a format or doing IO, and if it creates connections often does not maintain connections or keep files open between calls. 实用程序用于编码/解码格式或执行IO,如果创建连接,则通常不会保持连接或在调用之间保持文件打开。

  • A Manager is like a Helper but does have state between calls. 管理员就像一个助手,但确实在通话之间有状态。

  • A Factory is a class that gets or creates and then returns an object (either a custom one or an API one). Factory是一个获取或创建然后返回一个对象(自定义对象或API对象)的类。

  • Simple and Default often simply mean base class. 简单和默认通常只是指基类。

  • A My "class" tends to be for pre-flighting ideas and code examples, though it is sometimes used in production code eg for MyApplication and MyView (essentially to name singletons). 我的“类”往往用于预先传播的想法和代码示例,尽管它有时用于生产代码,例如MyApplication和MyView(主要用于命名单例)。

These class names are de facto. 这些类名实际上是。 These are meanings I create and see most often, but contradictory naming schemes and inconsistencies are often seen. 这些是我最常创建和看到的含义,但经常会出现矛盾的命名方案和不一致。 They are not formal design pattern names and in practice the choice of any of these names seems to be almost arbitrary. 它们不是正式的设计模式名称,在实践中,任何这些名称的选择似乎几乎是任意的。 Some people brand all their classes that are thread-safe with one of these names and those that are not with another of them. 有些人用它们中的一个来标记所有类的线程安全的类,以及那些与另一个不同的类。

Often too I see a "Manager" name is given to a class that does an ORM like function with objects or keys, or to an object that arbitrates connections. 通常我也会看到一个“管理器”名称被赋予一个类,该类使用对象或键执行ORM函数,或者对仲裁连接的对象执行。

EDIT 编辑

I see an app that's built on a framework as usually having these main objects: 我看到一个构建在框架上的应用程序通常具有以下主要对象:

  • Official process entry/exit/events handling stub 官方进程进入/退出/事件处理存根
  • A (singleton) application object (references a hierarchical data model and perhaps task objects) 一个(单例)应用程序对象(引用分层数据模型和可能的任务对象)
  • A database manager 数据库管理员
  • A network manager 网络管理员
  • A UI (that references or doesn't reference a view model depending on your religious persuasion) UI(根据您的宗教信仰引用或不引用视图模型)
  • Helper/Utility/Factory classes 助手/公用事业/工厂类
  • Miscellaneous glue code 杂项胶水代码
  • Ancillary files 辅助文件

I see focussing on maximising the testability and minimising the surface area of these interfaces as more important than package naming and file naming; 我认为专注于最大化可测试性并最小化这些接口的表面积比包命名和文件命名更重要; I think you should follow your own nose for the detailed breakdowns and naming for your project. 我认为您应该按照自己的想法为您的项目进行详细的细分和命名。 The splitting of code into different files for SCM purposes is specially important for shared files depended on by more than one bullet above. 将代码拆分为用于SCM目的的不同文件对于上面多个子弹所依赖的共享文件特别重要。

EDIT 编辑

I use singleton, flyweight, composite, iterator, memento, observer, state, strategy as a matter of routine. 我使用单身,飞重,复合,迭代器,纪念品,观察者,状态,策略作为例行事项。 I use facade, proxy, chain of responsibility between modules and in UI code. 我使用Facade,代理,模块之间和UI代码中的责任链。 I occasionally use factory, prototype and builder in complex data systems, and template and visitor when a system is particularly complex conceptually. 我偶尔会在复杂的数据系统中使用工厂,原型和构建器,在系统特别复杂的概念上使用模板和访问者。 I occasionally use adapter, bridge, factory, abstract factory, decorator when behaviour is complex. 当行为复杂时,偶尔会使用适配器,桥接器,工厂,抽象工厂,装饰器。 I rarely use Interpreter and I use mediator to help me write more general code in certain cases. 我很少使用Interpreter,我使用mediator来帮助我在某些情况下编写更多通用代码。 I don't personally like to name my classes after GoF but lots of people are very happy to, it can be a good idea and I am not against the practice. 我个人并不喜欢在GoF之后命名我的课程,但很多人都很高兴,这可能是一个好主意,我并不反对这种做法。 It can be very useful and if it makes people happy and it does help make clear to everyone what is going on in a particular instance then that's great. 它可以是非常有用的,如果它让人们开心,它确实帮助每个人清楚地知道在特定情况下发生了什么,那就太棒了。

I just feel that calling my app object a Singleton, Composite or ChainOfResponsibilityLink (?) does not feel good, and that calling my network and database code Adapters does not feel good so I call them Managers. 我只是觉得调用我的app对象是Singleton,Composite或ChainOfResponsibilityLink(?)感觉并不好,并且调用我的网络和数据库代码Adapters感觉并不好,所以我称之为Managers。 And I call a lot of things that ought to perhaps be called Facades under GoF, Helpers or Utilities because I think it is clearer what is meant. 而且我在GoF,Helpers或Utilities下调用了许多应该称为Facades的东西,因为我觉得它更清楚意味着什么。

You generally see a lot of these terms thrown around, but there aren't any official standards on that. 你通常会看到很多这些术语被抛出,但没有任何官方标准。 I would actually say that some of these are bad practices. 我实际上会说其中一些是不好的做法。 A lot of times "helper" and "manager" classes are classes that do too much and are a catch all for behavior that should be elsewhere. 很多时候“帮助”和“经理”类都是做得太多的类,并且应该是其他地方的行为。

To be honest, I am not sure what you mean by most of these terms. 说实话,我不确定大多数这些术语的含义。 If you are talking about design patterns then I believe the Gang of Four book ( Design Patterns ) has diagrams of each of the patterns that they use. 如果你在谈论设计模式,那么我相信四人帮书( 设计模式 )有他们使用的每个模式的图表。 If you are talking about something else, you might be overcomplicating things. 如果你在谈论其他事情,你可能会过度复杂化。

Also, where would you expect to get an "official" definition on terms that are not themselves official? 此外,您希望在哪些地方获得“官方”定义,而这些定义本身并不是正式的?

I don't think you'll find "official" definitions of these terms because they mean different things to different people. 我不认为你会发现这些术语的“官方”定义,因为它们对不同的人意味着不同的东西。 Names can be hard because while they are ultimately arbitrary, having a name that precisely and succinctly describe a class' role is a good goal to have. 名字可能很难,因为虽然它们最终是任意的,但是有一个名称可以精确而简洁地描述一个类的角色是一个很好的目标。 The GoF Design Patterns book is a good suggestion. GoF设计模式书是一个很好的建议。 You might also want to check out Core J2EE Patterns if you want something more Java-centric. 如果您想要更加以Java为中心的东西,您可能还想查看核心J2EE模式。

从我所看到的,项目的包布局更加依赖于代码的功能,而不是它的类型(即java.awt.image,java.awt.font而不是java.awt.utils, java.awt.singletons)。

There really isn't a standard for naming packages and deciding what types of classes live in those packages. 实际上没有标准用于命名包并决定这些包中存在哪些类型的类。 It's all based on your preferences and what makes sense to you. 这完全取决于您的偏好以及对您有意义的事情。 Although in a group environment package naming and what types of classes belong in each package would typically be decided during design meetings where the group can reach a decision pretty much by consensuses. 虽然在组环境中,包命名和每个包中属于哪种类型的类通常会在设计会议期间决定,其中组可以通过共识来达成决策。 Or you could just work for a control freak that what things done their way or no way and they decide for you. 或者你可以为一个控制狂来工作,他们做了什么事情或者没办法,他们为你决定。

I think any class called a FooManager is at best questionable design in an OO system. 我认为任何一个叫做FooManager的类都是OO系统中最值得怀疑的设计。 They tend to be places were tons of state and lots of global variables are kept. 它们往往是大量州的地方,并保留了许多全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM