接口(接口/抽象类)不是抽象? [英] Interfaces (interface/abstract class) are not abstractions?
问题描述
最近,我一直在阅读关于接口是抽象的错误观念的文章.一个这样的帖子是 http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx
Of late, I have been reading posts which talks about the supposed wrong notion that interfaces are abstractions. One such post is http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstractions.aspx
我有点困惑.如果我没有接口(接口/抽象类),那么我将如何注入我的依赖项并模拟它们?
I am a bit confused. If I don't have interfaces (interface/abstract class), then how will I inject my dependencies and mock them?
此外,我听说有人谈论不要使用只有一个实现者的接口.喜欢这里的博客 - http://simpleprogrammer.com/2010/11/02/back-to-basics-what-is-an-interface/
Also, I have heard people talk about not using interfaces which has just one implementor. Like this blog here - http://simpleprogrammer.com/2010/11/02/back-to-basics-what-is-an-interface/
现在这一切,是不是违反了原则——为接口编程而不是实现?
Now all this, doesn't it violate the principle - Program to an interface and not implementation?
推荐答案
编程到接口而不是实现更多的是使用数据抽象和封装.
Programming to an interface instead of an implementation is more about using data abstraction and encapsulation.
当我们在对接口进行编程时说接口".这种接口意味着类的面向外部的方法和属性.它不必是语言级别的界面.(关键字接口.)
When we say "interface" in terms of programming to an interface. That kind of interface means the external facing methods and properties of a class. It doesn't have to be a language level interface. (The keyword interface.)
您应该努力确保您的代码不依赖于其他类的内部细节.
You should be striving to make sure that your code is not dependent on the internal details of other classes.
这篇关于接口(接口/抽象类)不是抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!