面向接口编程。如何决定其需要的地方? [英] Programming to an interface. How to decide where its needed?

查看:216
本文介绍了面向接口编程。如何决定其需要的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,面向接口编程有助于松散耦合。但是,有没有解释的指导方针时,其最有效的?

I understand that programming to interfaces helps with loose coupling. However, is there a guideline that explains when its most effective?

例如,我有一个收集有关的员工,他们的训练计划,费用并计算数据的简单Web应用程序他们对今年的费用等等。这是一个相当简单的应用程序,当然我可以用一个接口,但不知道是否会有任何用处。我将用它来使用它的缘故。

For example, I have a simple web application that collects data on employees, their training plans, expenses and computes their expense for the year etc. This is a fairly simple application and I can of course use an interface but wonder if there would be any use. I will be using it for the sake of using it.

它总是可以这样说,随着应用程序的增长的复杂性,我绕过物体会更有意义通过比一个实现类型(接口)。所以,我应该等待应用程序变得复杂或使用它,对吗?我想知道最好的做法可能会变成是这家伙在做的事情。

It can always be argued that as the application grows in complexity and I pass around objects it would make more sense to pass the type (interface) than an implementation. So, should I wait for the application to become complex or use it right away? I'm wondering the best practice might turn out to be "this guy is over doing things".

推荐答案

如果你有机会自己感兴趣的测试驱动开发,需要进行面向接口编程真的变得很明显。如果你想有一个类在其依赖的独立测试,你需要绕过接口,而不是对象。

If you ever get yourself interested in Test Driven Development, the need for "programming to an interface" really becomes apparent. If you want a class to be tested in isolation of its dependencies, you need to pass around interfaces instead of objects.

这篇关于面向接口编程。如何决定其需要的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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