Dart - 使用 Mixins 与依赖注入

Dus*_*tap 6 dart flutter

使用 mixin 与依赖注入时有区别(就良好的设计而言)吗?

我对 Dart 比较陌生,这是我第一次了解 mixins。

使用混合:

mixin Foo {
  bar() => print('Bar!');
}

class MyClass with Foo {
  doBar() => bar();
}

Run Code Online (Sandbox Code Playgroud)

使用DI:

abstract class FooService {
  bar();
}

class FooServiceImpl implements FooService {
  @override
  bar() => print('Bar!');
}

class MyClass {
  MyClass(this.fooService);

  final FooService fooService;

  doBar() => fooService.bar();
}
Run Code Online (Sandbox Code Playgroud)