它是提取每类的接口的最佳做法? [英] Is it the best practice to extract an interface for every class?

查看:88
本文介绍了它是提取每类的接口的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过code其中每个类都有它实现了一个接口。

I have seen code where every class has an interface that it implements.

有时,他们都没有通用接口。

Sometimes there is no common interface for them all.

他们只是在那里,他们被用来代替混凝土的对象。

They are just there and they are used instead of concrete objects.

他们不提供的通用接口为两个类和特定于该问题的域该类求解

They do not offer a generic interface for two classes and are specific to the domain of the problem that the class solves.

是否有任何理由这样做吗?

Is there any reason to do that?

推荐答案

<子>重新审视这个答案之后,我决定稍微修改它。

没有,它不是以提取的每个的类接口的最佳实践。这当然会适得其反。然而,接口是有几个原因非常有用:

No, it's not best practice to extract interfaces for every class. This can actually be counterproductive. However, interfaces are useful for a few reasons:


  • 测试支持(嘲笑,存根)。

  • 实施抽象(进一步到的IoC / DI)。

  • 辅助之类的东西在C#中合作和反方差支持。

有关实现这些目标,接口的认为的好习惯(和实际需要的最后一个点)。根据项目的大小,你会发现,你可能永远需要交谈的界面,或者你不断地抽取接口,用于上述原因之一。

For achieving these goals, interfaces are considered good practice (and are actually required for the last point). Depending on the project size, you will find that you may never need talk to an interface or that you are constantly extracting interfaces for one of the above reasons.

我们维持一个大的应用程序,它的某些部分是伟大的,有些是从没有注意的痛苦。我们经常发现自己重构拉接口出类型,使其可测试或使我们能够改变的实现,同时减轻这种变化的影响。我们也这样做是为了减少耦合的效果,如果你是在你的公开API不严格具体的类型可能会意外地强加(接口只能重新present一个公共API,因此对我们来说本来变得相当严格)。

We maintain a large application, some parts of it are great and some are suffering from lack of attention. We frequently find ourselves refactoring to pull an interface out of a type to make it testable or so we can change implementations whilst lessening the impact of that change. We also do this to reduce the "coupling" effect that concrete types can accidentally impose if you are not strict on your public API (interfaces can only represent a public API so for us inherently become quite strict).

这就是说,它可以在不接口抽象行为和可能的,而不需要的接口,以测试的类型,所以它们不是一个的需求的上述内容。它就是这样,你可以用它来支持你的任务大多数的框架/库将针对接口有效地运作。



<子>我会离开我的旧的答案环境。

That said, it is possible to abstract behaviour without interfaces and possible to test types without needing interfaces, so they are not a requirement to the above. It is just that most frameworks / libraries that you may use to support you in those tasks will operate effectively against interfaces.


I'll leave my old answer for context.

接口定义了一个公共合同。实现接口的人们必须执行这一合同。消费者只看到公共合同。这意味着,实施细则一直的抽离的从消费者。

Interfaces define a public contract. People implementing interfaces have to implement this contract. Consumers only see the public contract. This means the implementation details have been abstracted away from the consumer.

此,这些天立即使用的 单元测试。接口很容易嘲笑,存根,假的,你的名字。

An immediate use for this these days is Unit Testing. Interfaces are easy to mock, stub, fake, you name it.

另一个立即使用的依赖关系注入。对于给定接口的注册具体类型被提供给一个类型消耗的接口。该类型并不了解执行情况,特别关心,因此它可以抽象地要求的接口。这允许您更改实现,而不会影响大量code的(影响面积非常小,只要合同保持不变)。

Another immediate use is Dependency Injection. A registered concrete type for a given interface is provided to a type consuming an interface. The type doesn't care specifically about the implementation, so it can abstractly ask for the interface. This allows you to change implementations without impacting lots of code (the impact area is very small so long as the contract stays the same).

对于非常小的项目,我倾向于不要打扰,中等项目,我倾向于打扰重要的核心项目,大项目也往往是,几乎每一个类的接口。这几乎总是支持测试,但在注射的行为或行为的抽象化某些情况下减少code重复。

For very small projects I tend not to bother, for medium projects I tend to bother on important core items, and for large projects there tends to be an interface for almost every class. This is almost always to support testing, but in some cases of injected behaviour, or abstraction of behaviour to reduce code duplication.

这篇关于它是提取每类的接口的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆