是否每个对象都有一个接口,所有对象都松散耦合? [英] Should every single object have an interface and all objects loosely coupled?
问题描述
从我所读的最佳实践是基于接口的类和松散耦合对象,以帮助代码重用和单元测试。
From what I have read best practice is to have classes based on an interface and loosely couple the objects, in order to help code re-use and unit test.
这是正确的,是否应该始终遵循规则?
Is this correct and is it a rule that should always be followed?
我问的原因是我最近在一个系统中使用了100个非常不同的对象。一些共享的公共接口,但大多数都没有,并想知道它是否应该有一个镜像镜像这些类中的每个属性和函数?
The reason I ask is I have recently worked on a system with 100’s of very different objects. A few shared common interfaces but most do not and wonder if it should have had an interface mirroring every property and function in those classes?
我正在使用C#和dot net 2.0但我相信这个问题适合多种语言。
I am using C# and dot net 2.0 however I believe this question would fit many languages.
推荐答案
它对真正提供服务的对象很有用 - 身份验证,存储等。对于没有任何进一步的简单类型依赖关系,以及永远不会有任何替代实现的地方,我认为使用具体类型是可以的。
It's useful for objects which really provide a service - authentication, storage etc. For simple types which don't have any further dependencies, and where there are never going to be any alternative implementations, I think it's okay to use the concrete types.
如果你对这种事情过火了,你最终花费了很多时间嘲笑/抄袭世界上的所有东西 - 这通常最终会导致脆弱的测试。
If you go overboard with this kind of thing, you end up spending a lot of time mocking/stubbing everything in the world - which can often end up creating brittle tests.
这篇关于是否每个对象都有一个接口,所有对象都松散耦合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!