Are Interfaces just “Syntactic Sugar”?

2019-03-26 07:34发布

I've been playing mostly with PHP and Python.

I've been reading about Interfaces in OO programming and can't see an advantage in using it.

Multiple objects can implement the same interface, but multiple inheritance doesn't provide this as well?

Why do I need to create an Interface "with no implementation" - mainly a "contract" - if I can just check if a method exists in an object in Python, that inherits from multiple classes?

Do Interfaces were created in another languages because they don't provide multiple inheritance? Or am I missing something more important here?

9条回答
做个烂人
2楼-- · 2019-03-26 08:10

Please read Twisted Framework article about power of Zope Interfaces in python.

查看更多
我命由我不由天
3楼-- · 2019-03-26 08:12

Even in duck-typed languages like Python, an interface can be a clearer statement of your intent. If you have a number of implementations, and they share a set of methods, an interface can be a good way to document the external behavior of those methods, give the concept a name, and make the concept concrete.

Without the explicit interface, there's an important concept in your system that has no physical representation. This doesn't mean you have to use interfaces, but interfaces provide that concreteness.

查看更多
Emotional °昔
4楼-- · 2019-03-26 08:13

The usefulness of an interface is directly connected to the usefulness of static typing. If you're working in a dynamically-typed language like PHP or Python, interfaces truly don't add significantly to the expressiveness of the language. That is, any program that can be described as using interfaces can be expressed without significant difference without using interfaces.

As a result, Python has a fairly nebulous concept of a "protocol" (an implementation conforming to a certain pattern, like the iteration protocol) which amounts to essentially the same thing, but without the other benefits of compile-time checking its value is limited.

In a statically-typed language, on the other hand, an interface is essential to allow implementation to be decoupled from implementation. In a static language, the types of all expressions must be resolved at compile time, so normally bindings to implementation must be made at that time, limiting run-time flexibility. An interface defines how to access functionality without defining a specific implementation, which allows a static language to prove that expressions are correct without having access to the implementation.

Without interfaces (or an equivalent formulation like C++'s pure virtual functions), the expressiveness of a statically-typed language would be severely hampered. In fact, many implementations exist (Win32 and COM come immediately to mind) to essentially reproduce much of the functionality of interfaces and virtual dispatch in C by storing function pointers in structures (and thus re-implementing C++'s virtual functions and vtable invocation by hand). In this case there is a big difference in expressiveness, since many changes are required in the program to express the same concepts.

Interfaces are just one example of type polymorphism, and a fairly limited one at that. In languages that support parametric polymorphism (aka generics) you can accomplish much more. (For example, C#'s LINQ would not be possible without generic interfaces.) For a much more powerful form of the same kind of thing, look into Haskell's typeclasses.

查看更多
Ridiculous、
5楼-- · 2019-03-26 08:18

Because you want to program against an interface and not a concrete implementation (GoF 1995:18)

查看更多
可以哭但决不认输i
6楼-- · 2019-03-26 08:19

Because sometimes you don't want to provide an implementation.

Java Interface

Java class

查看更多
祖国的老花朵
7楼-- · 2019-03-26 08:19

Yes. As for PHP, interfaces are just a means to overcome the lack of multiple inheritance. There are minor semantic differences useful for IDEs, and fewer conflicts caused by interfaces clearly aid newbie programmers. But as said before, it's not strictly necessary in dynamic languages. http://c2.com/cgi/wiki?MultipleInheritance

查看更多
登录 后发表回答