动态语言界面?

发布于 2024-08-29 22:58:13 字数 272 浏览 2 评论 0原文

接口(或者所有方法都是抽象的抽象类)是C#、JAVA等静态类型语言的有力武器。它允许以统一的方式使用不同的派生类型。设计模式鼓励我们尽可能多地使用界面。

但是,在动态类型语言中,不会在编译时检查所有对象的类型。他们不必实现以特定方式使用的接口。您只需要确保它们定义了一些方法(属性)。这使得接口变得不必要,或者至少不如静态语言中的那么有用。

典型的动态语言(例如ruby)有接口吗?如果是的话,那么拥有它有什么好处呢?如果没有,那么我们是否会失去许多需要界面的美丽设计模式?

谢谢。

Interface (or an abstract class with all the methods abstract) is a powerful weapon in a static-typed language such as C#, JAVA. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来是傀儡 2024-09-05 22:58:13

我想所有动态语言都没有单一的答案。例如,在Python中,没有接口,但有多重继承。使用类似接口的类仍然有用:

  • 类似接口的类可以提供方法的默认实现;
  • 鸭子打字很好,但在一定程度上;有时能够编写 isinstance(x, SomeType) 很有用,特别是当 SomeType 包含许多方法时。

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.
再可℃爱ぅ一点好了 2024-09-05 22:58:13

动态语言中的接口可用作可自动检查的 API 文档,例如通过开发工具或运行时断言。

例如,zope.interface 是 Python 中接口的事实上的标准。像 Zope 和 Twisted 这样公开大量 API 供使用的项目发现它很有用,但据我所知,它在此类项目之外的使用不多。

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.

兮颜 2024-09-05 22:58:13

在Ruby这种动态类型语言中,只允许单继承,你可以通过mixin来模拟“接口”,而不是用“接口”的方法来污染类。

Mixin 部分模仿多重继承,允许对象从多个源“继承”,但没有实际具有多个父对象的歧义和复杂性。真正的父母只有一位。

要实现接口(在抽象意义上,不是静态类型语言中的实际接口类型),您可以将模块定义为静态语言中的接口。然后您将其包含在类中。瞧!您已将鸭子类型收集到本质上是一个接口中。

非常简单的示例:

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 是 Hero、Mount 以及包含它的任何其他类或模型的接口。

(显然,武器很可能由初始化器动态设置,在本例中已对其进行了简化。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文