接口(接口/抽象类)不是抽象的? [英] 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屋!