服务和DAO始终实现接口 [英] Service and DAO always implement interfaces
问题描述
在我见过的所有MVC项目中,服务"和"DAO"类始终实现自己的接口.但是几乎所有时候,我都没有看到过使用此界面有用的情况.
In all the MVC projects I've seen, "service" and "DAO" classes always implemented their own interfaces. But almost all the times, I haven't seen a situation in which having this interface has been useful.
在这些情况下是否有任何使用接口的理由?在服务"和"DAO"类中不使用接口可能会有什么后果?我无法想象会有任何后果.
Is there any reason to use interfaces in these cases? What may be the consequence of not using interfaces in "service" and "DAO" classes? I can't imagine any consequences.
推荐答案
有很多支持接口的参数,请参见Google.
There are lots of arguments in favour of interfaces, see Google.
我可以添加其他人提到的观点:
I can added to the points other people mentioned:
- 想象一下,您将DAO实现从Hibernate更改为iBatis.依赖接口而不是实现将对服务层有很大帮助.
- 如果您使用AOP或使用JDK动态代理的代理,则您的类必须实现接口. CGLIB并非如此.
- 在服务层中,如果您想将方法释放给其他客户端以进行调用,则将它们作为接口作为合同"给予他们比实现更有意义.
- 如果您想将services.jar与daos.jar分开,则在daos上具有接口可以避免在daos.jar发生更改的情况下重新编译services.jar.
总之,拥有接口是很好的!
In short, it is just good to have interfaces!
这篇关于服务和DAO始终实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!