开发者

Interface in a dynamic language?

开发者 https://www.devze.com 2022-12-27 13:31 出处:网络
Interface (or an abstract class with all the methods abstract) is a powerful weapon in a static-typed language such as C#, JAV开发者_开发百科A. It allows different derived types to be used in a unifor

Interface (or an abstract class with all the methods abstract) is a powerful weapon in a static-typed language such as C#, JAV开发者_开发百科A. It allows different derived types to be used in a uniformed way. Design patterns encourage us to use interface as much as possible.

However, in a dynamic-typed language, all objects are not checked for their type at compile time. They don't have to implement an interface to be used in a specific way. You just need to make sure that they have some methods (attributes) defined. This makes interface not necessary, or at least not as useful as it is in a static language.

Does a typical dynamic language (e.g. ruby) have interface? If it does, then what are the benefits of having it? If it doesn't, then are we losing many of the beautiful design patterns that require an interface?

Thanks.


I guess there is no single answer for all dynamic languages. In Python, for instance, there are no interfaces, but there is multiple inheritance. Using interface-like classes is still useful:

  • Interface-like classes can provide default implementation of methods;
  • Duck-typing is good, but to an extent; sometimes it is useful to be able to write isinstance(x, SomeType), especially when SomeType contains many methods.


Interfaces in dynamic languages are useful as documentation of APIs that can be checked automatically, e.g. by development tools or asserts at runtime.

As an example, zope.interface is the de-facto standard for interfaces in Python. Projects such as Zope and Twisted that expose huge APIs for consumption find it useful, but as far as I know it's not used much outside this type of projects.


In Ruby, which is a dynamically-typed language and only allows single inheritance, you can mimic an "interface" via mixins, rather than polluting the class with the methods of the "interface".

Mixins partially mimic multiple inheritance, allowing an object to "inherit" from multiple sources, but without the ambiguity and complexity of actually having multiple parents. There is only one true parent.

To implement an interface (in the abstract sense, not an actual interface type as in statically-typed languages) You define a module as if it were an interface in a static language. You then include it in the class. Voila! You've gathered the duck type into what is essentially an interface.

Very simplified example:

module Equippable
  def weapon
    "broadsword"
  end
end


class Hero
  include Equippable

  def hero_method_1
  end

  def hero_method_2
  end
end


class Mount
  include Equippable

  def mount_method_1
  end
end


h = Hero.new
h.weapon    # outputs "broadsword"


m = Mount.new
m.weapon    # outputs "broadsword"

Equippable is the interface for Hero, Mount, and any other class or model that includes it.

(Obviously, the weapon will most likely be dynamically set by an initializer, which has been simplified away in this example.)

0

精彩评论

暂无评论...
验证码 换一张
取 消