且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么要使用Interface?只是标准化吗?

更新时间:2023-11-06 10:18:04

/ h2>


  • 创建松散耦合的软件

  • 支持按合同设计(一个实现者必须提供整个接口) li>
  • 允许可插入的软件

  • 允许不同的对象轻松交互

  • 相互隐藏类的实现细节

  • 便于软件重用



类比1 :很像美国航天飞机,俄罗斯联盟号航天器和中国神舟5号都可以停靠国际空间站,因为它们实现了相同的对接界面。 (这只是一个例子 - 我不知道在现实生活中是否真实,但是为了举个例子,我们暂停我们的怀疑)



类比2 :像您可以将各种电脑显示器插入家用电脑。您可以将壁挂式电视插入其中,一个旧的CRT(厚型),一个20平面屏幕或盲文机,让盲人通过触摸查看。这些各种/不同设备和您的因为他们都同意接口标准。



C#界面的详细信息 -
使用C#/ OOP接口,您正在做同样的事情,但看不见/虚拟世界。



您正确的标准化,而且可变性可扩展性可扩展性可维护性可重用性可测试性电源。 >

(使用软件界面越多,这些嗡嗡声就越能被理解,并且总是考虑现实世界中的接口,因为他们已经做了我们亲爱的。)


Why do we use Interface?

Is it only for Standardization?

Purposes of Interfaces

  • create loosely coupled software
  • support design by contract (an implementor must provide the entire interface)
  • allow for pluggable software
  • allow different objects to interact easily
  • hide implementation details of classes from each other
  • facilitate reuse of software

Analogy 1: Much like the US space shuttle, Russian Soyuz spacecraft and Chinese Shenzhou 5 can all dock to the International Space Station, because they implement the same docking interface. (This is just an example - I don't know if it's true in real life however let's suspend our disbelief for the sake of an example)

Analogy 2: Like you can plug various computer monitors into your home computer. You can plug a wall-size TV into it, an old CRT (the thick kind), a 20" flat screen, or a braille machine for the blind to "see" by touch. There's compatibility among these various/different devices and your computer because they all agree on interface standards.

Details of C# interfaces -- With C#/OOP interfaces you're doing the same kind of thing but in the unseen/virtual world.

You're correct about standardization, but also flexibility, scalability, extensibility, maintainability, reusability, testability and power.

(The more you use software interfaces the more these "buzz words" will be understood. And always consider interfaces in the real world because they have done us equally well.)