什么时候在Dart中使用接口? [英] When to use interfaces in Dart?

查看:194
本文介绍了什么时候在Dart中使用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Dart的文档,我有点困惑,也许是因为我来自Ruby,如何使用界面。当然,接口不是Dart独有的,有很多解释,当有人应该使用一个接口。例如,这一个似乎在说,接口只有当你'在一个团队。甚至在开放源代码世界里,每个人都读取和重用别人的代码,是什么意思?



我看到的一个有趣的解释似乎意味着接口使用:a)在缺少多重继承的语言中,b)对于这种情况,它们可以作为多重继承缺失的解决方法。



我不明白。我理解Ruby中的模块是一个解决方法,因为它们允许我使用实体定义实际方法。接口只允许我定义实现它的类应该有什么方法。有什么收获?任何人都可以告诉我一个真正有用的例子,我可以立即看到使用接口的价值。



在相关的说明,是否有一种方法使用Dart中的多重继承?

解决方案

接口是有用的,



采取以下(常用的)示例:

 接口Quackable {
void quack
}

这定义了一个类的要求,该类将被传递给一个方法,例如:

  sayQuack(Quackable quackable){
quackable.quack
}

它允许您使用Quackable对象的任何实现,例如:

  class MockDuck implements Quackable {
void quack() print(quack);
}

class EnterpriseDuck实现Quackable {
void quack(){
//连接到三个企业池
//并吃一些服务器bread
//并使用消息系统说quack
}

}

这两个实现都将与sayQuack()函数一起工作,但是一个需要比其他实现更少的基础设施。

  sayQuack(new EnterpriseDuck()); 
sayQuack(new MockDuck());

我在Java世界中使用这种模式,当构建使用一些企业鸭。在本地开发时,我只需要能够调用sayQuack()函数并返回一些硬编码的模拟数据。



/ strong>



因为Dart是可选类型的,你实际上不需要使用接口,只需写一个包含正确方法签名的类就可以工作工具将无法验证它。)

  class Person {// note:no implements关键字
void quack()=> 我不是鸭子;
}

sayQuack(new Person()); //提供quack方法,所以这仍然可以工作

strong>



最后,所有类都是接口。这意味着即使第三方系统可能没有使用接口编写,您仍然可以使用一个具体类,就像它是一个接口。



例如,假设以下企业库:

  class EnterpriseDuck {// note:no implements关键字
void quack(){
// snip
}
}

sayQuack (EnterpriseDuck duck){//获取EnterpriseDuck类的实例
duck.quack();
}

而且你想通过一个模拟鸭子到sayQuack方法类型检查器可以验证。你可以创建你的mockDuck来实现EnterpriseDuck隐含的接口,只需使用EnterpriseDuck作为一个接口:

  class MockDuck implements EnterpriseDuck {
void quack()=> 我是一个模拟企业鸭;
}

多重继承



在多重继承方面,这在Dart中是不可能的。但是,您可以实现多个接口并提供所需方法的自己实现,例如:

  class MultiDuck implements Quackable,EnterpriseDuck,Swimable {
// snip ...
}


$ b b

接口可以有默认类



当你使用Dart时,你会发现大多数类 List,String等等都是提供的默认实现的接口。当你调用

  List myList = new List(); 

你实际上是使用List接口,新关键字从界面重定向到底层默认List



接口在以下方面非常有用:

团队开发,甚至在开源世界。该接口定义了您应该构建的方法和属性,以便您的组件与我的组件一起使用。你可以构建自己的测试实现的接口,我可以构建我的具体实现的接口,当我们完成后,我们可以集成。没有公布的共享接口,我需要提供具体的实现,然后才能真正开始。



希望有所帮助!


I was reading Dart's documentation and I was a little bit confused, maybe because I'm coming from Ruby, as to how to use interfaces. Of course, interfaces are not unique to Dart and there are quite a number of explanations out there on when one should use an interface. This one, for example, seems to be saying that interfaces are only useful when you're in a team. What is it even supposed to mean in the opensource world, where everybody reads and reuses somebody else's code?

One interesting explanation I've seen seemed to be implying that interfaces are used: a) in languages that lack multiple inheritance and b) for that matter they somehow serve as a workaround for multiple inheritance absence.

I don't understand that. I understand that modules in Ruby is a workaround, because they allow me to define real methods with actual bodies. Interfaces only allow me to define what methods a class implementing it should have. What's the catch? Can anyone tell of a real useful example where I can immediately see the value of using interfaces?

P.S. On a related note, is there a way to use multiple inheritance in Dart?

解决方案

Interfaces are useful because they allow you to switch implementations of a class, whilst still allowing validation that the type being passed in meets the requirements of the interface.

Take the following (often used) example:

interface Quackable {
  void quack();
}

This defines the requirements of a class that will be passed to a method such as:

sayQuack(Quackable quackable) {
   quackable.quack();
}

which allows you to make use of any implementation of a Quackable object, such as:

class MockDuck implements Quackable {
  void quack() => print("quack");
}

class EnterpriseDuck implements Quackable {
  void quack() {
    // connect to three enterprise "ponds"
    // and eat some server bread
    // and say "quack" using an messaging system
  }

}

Both of these implementations will work with the sayQuack() function, but one requires significantly less infrastructure than the other.

sayQuack(new EnterpriseDuck());
sayQuack(new MockDuck());

I use this pattern all the time in the Java world, when building solutions that make use of some "enterprise duck". When developing locally, all I simply need is to be able to call the sayQuack() function and return some hard-coded, mock data.

Duck typing

Because Dart is optionally typed, you don't actually need to use the interface, simply writing a class that contains the correct method signature will work (although the tools won't be able to validate it).

class Person {   // note: no implements keyword
  void quack() => "I'm not a duck";
}

sayQuack(new Person()); // provides the quack method, so this will still work

All classes are interfaces

Finally, all Classes are also interfaces. This means that even though a third party system may have been written without using interfaces, you can still use a concrete class as though it were an interface.

For example, imagine the following enterprise library:

class EnterpriseDuck { // note: no implements keyword
  void quack() {
    // snip
  }
}

sayQuack(EnterpriseDuck duck) {  // takes an instance of the EnterpriseDuck class
  duck.quack();
}

And you want to pass a mock duck into the sayQuack method in a way that the type checker can validate. You can create your mockDuck to implement the interface implied by EnterpriseDuck, simply by using the EnterpriseDuck as an interface:

class MockDuck implements EnterpriseDuck {
  void quack() => "I'm a mock enterprise duck";
}

Multiple Inheritance

In terms of multiple inheritance, this is not possible in Dart. You can, however, implement multiple interfaces and provide your own implementations of the required methods, eg:

class MultiDuck implements Quackable, EnterpriseDuck, Swimable {
  // snip...
}

Interfaces can have default classes

As you use Dart, you will find that most "classes" are actually interfaces. List, String etc... are all interfaces with default implementations provided. When you call

List myList = new List();

you are actually using a List interface, and the new keyword redirects from the interface to an underlying default List implementation.

With regards to developing in a team

Interfaces are useful in team development, even in the open source world. The interface defines the methods and properties that you should be building so that your component works with my component. You can build your own test implementation of that interface, and I can build my concrete implementation of that interface, and when we're both done, we can integrate. Without the published, shared interface, I would need to provide my concrete implementation before you could really get started.

Hope that helps!

这篇关于什么时候在Dart中使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆