使用 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)
| 归档时间: |
|
| 查看次数: |
564 次 |
| 最近记录: |