接口和耦合
从设计和松耦合的角度来看。为项目中可能属于组合模型一部分的每个类提供一个接口是个好主意吗?
我有一个项目正在这样做,但现在我得到了相当多的接口,试图保持事物相对松散的耦合。
From a design and loose coupling standpoint. Is it a good idea to have an interface for each class in a project that might be part of a composition model?
I have a project where I'm doing this, but now I'm getting rather a lot of interfaces, in an attempt to keep things relatively loosely coupled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在不了解设计细节的情况下,接口隔离原则 (pdf) 就是这样的应该可以工作。
您应该为可能需要替换其实现的每个类提供一个接口(例如,我不会为每个 DTO 创建一个接口)。
Without knowing specifics of your design, that's how the Interface Segregation Principle (pdf) is supposed to work.
You should provide an interface for every class that you may need to swap out the implementation for (I wouldn't create an interface for each DTO, for example).
我通常会创建接口来松散耦合类进行测试,以便我可以为我不感兴趣的测试类创建假冒产品。例如,业务逻辑管理器类将具有对数据访问类的接口的引用。
我仅在测试确实需要“接缝”时才创建接口,我不只是为所有内容创建接口。
I generally create interfaces to loosely couple classes for testing so that I can create fakes for the classes I'm not interested in testing. EG a business logic manager class will have a reference to an interface for a data access class.
I only create an interface if I actually need a 'seam' for my tests, I don't just create interfaces for everything.