简体   繁体   English

每个Java域类应该实现一个接口吗?

[英]Should every Java domain class implement an interface?

I've been looking over another Java project done at my company and in this project the developers created an interface for pretty much ever domain entity (there are hundreds). 我一直在寻找在我的公司完成的另一个Java项目,在这个项目中,开发人员为几乎所有的域实体创建了一个接口(有数百个)。 In some cases, I think the abstraction works, but in other cases it doesn't seem like it is need at this time. 在某些情况下,我认为抽象是有效的,但在其他情况下,它似乎并不像现在这样需要。

Whenever instances are passed around they are always referred and access via the interface. 每当传递实例时,它们总是被引用并通过接口访问。

Is this bloat from too much future proofing? 这种臃肿是否会受到太多未来的证明? or is this sound engineering practice? 或者这是合理的工程实践吗?

Primarily interfaces are obviously a design tool to unite disparate classes with common traits. 主要接口显然是一种设计工具,用于将不同的类与特定的特征联合起来。

But then interfaces like the ones mentioned can also help a lot when writing unit tests. 但是,在编写单元测试时,所提到的接口也可以提供很多帮助。 Tools like easymock works really well with interfaces. easymock这样的工具可以很好地处理接口。

Personally I like having interfaces for services, but not necessarily domain objects, depending a bit on how "rich" the domain objects are. 我个人喜欢有服务接口,但不一定是域对象,这取决于域对象的“丰富”程度。 If they, for instance, do a lot of filesystem stuff or have really close ties to the services/dao layer, I will probably make interfaces there too - to make easier unit tests. 例如,如果他们做了很多文件系统的东西,或者与services / dao层有很紧密的联系,我可能会在那里建立接口 - 以便更容易进行单元测试。

Interfaces specify required behavior without implementation. 接口指定所需的行为而不实现。 They allow you to swap implementations without affecting clients. 它们允许您交换实现而不影响客户端。 They can be especially useful for techniques like aspect-oriented programming or proxy generation. 它们对于面向方面编程或代理生成等技术尤其有用。

But if implementation doesn't change, I see no justification for an interface. 但是如果实现没有改变,我认为接口没有任何理由。

Most model objects fall into that category. 大多数模型对象都属于该类别。 If there's no implementation differences, don't use an interface. 如果没有实现差异,请不要使用接口。

Interfaces are terrific for services and persistence classes, but I have never seen them used to abstract model objects. 接口对于服务和持久化类来说非常棒,但我从未见过它们用于抽象模型对象。

A good rule of thumb: Don't introduce abstraction for abstraction's sake. 一个好的经验法则:不要为了抽象而引入抽象。 Only do it if it enables your project somehow. 只有在它以某种方式启用您的项目时才这样做。 If it makes your unit testing easier, or it makes upgrades easier, or enables dependency injection etc. Don't make your project any more complicated that is useful. 如果它使您的单元测试更容易,或者它使升级更容易,或启用依赖注入等。不要让您的项目更复杂,有用。

For domain objects, if they are POJOs then there really isn't any benefit to making interfaces out of them. 对于域对象,如果它们是POJO,那么从它们中创建接口确实没有任何好处。 If they are not POJOs then I would argue that they might not actually be domain objects... but I guess that is a different argument. 如果他们不是POJO那么我认为他们可能实际上不是域对象...但我想这是一个不同的论点。

I think they wanted to show off with complexity. 我认为他们想要炫耀复杂性。 You should only have an interface if you have several implementaions or to indicate that it would be an extension point. 如果您有多个实现或者表明它将是一个扩展点,您应该只有一个接口。

Using interfaces allow you to have multiple, independent implementations of the same functionality, which in turn allows you to put away implementation details. 使用接口可以让您拥有相同功能的多个独立实现,这反过来又可以让您放弃实现细节。

You might have public classes in your implementation that you could be tempted to use if you directly referred to the classes. 您可能在实现中有公共类,如果您直接引用这些类,可能会想要使用它们。 By using the interface and only that, you are guaranteed that the calling code does not trespas on what it is allowed to do. 通过使用接口,只有那样,您可以保证调用代码不会超出允许的范围。

You can do all kinds of advanced tricks when dealing with an interface. 处理界面时,您可以执行各种高级技巧。 Need to add debugging. 需要添加调试。 Write a wrapper that for each method does the logging and then calls the wrapped instance. 编写一个包装器,为每个方法执行日志记录,然后调用包装的实例。

The list goes on and on. 这个清单一直在继续。 Code to interfaces. 代码到接口。 your code will be better for it. 你的代码会更好。

No. If you say that an interface was created for every domain, then what is the point? 不。如果你说为每个域创建了一个接口,那么重点是什么? Interfaces are only actually useful in grouping several classes together and representing common behavior. 接口实际上只用于将几个类组合在一起并表示常见行为。

Abstraction is sometimes overrated and can be easily overdone. 抽象有时被高估,可能很容易过度。

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

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