Dart多态控制反转编码规范实例详解


前言

我们通常都知道程序设计要依赖抽象,提高复用性,做到对扩展开放,对修改关闭。贯彻SOLID五大原则的最重要法宝就是抽象和继承。多态是一种手段,下面,通过简单 demo 介绍 flutter 开发中常用的最佳实践。

举栗子🌰

/// 不推荐,避免把逻辑放在公共底层处理class TestWidget extends StatefulWidget {  const TestWidget({Key? key}) : super(key: key);  @override  TestWidgetState createState() => TestWidgetState();}class TestWidgetState extends State<TestWidget> {  dynamic param;  Widget childWidget = Container();  ///提供给外部调用  void update(dynamic value) {    setState(() {      param = value;      _updateBody();    });  }  void _updateBody(){    if(param == null){      childWidget = Container();      return;    }    switch(param.runtimeType){      case A:        childWidget = AWidget(a: param as A,);        break;      case B:        childWidget = BWidget(b: param as B,);        break;      default:        childWidget = Container();        break;    }  }  @override  Widget build(BuildContext context) {    return Container(      child: childWidget,    );  }}

如上的写法是不推荐的,应该进行依赖倒置,将可变的部分放上层处理,保证底层干净,如下:

方式一:通过传参构造器进行控制反转

typedef ChildBuilder = Widget Function(dynamic param);class TestWidget extends StatefulWidget {  final ChildBuilder builder;  const TestWidget({Key? key, required this.builder}) : super(key: key);  @override  TestWidgetState createState() => TestWidgetState();}class TestWidgetState extends State<TestWidget> {  dynamic param;  void update(dynamic value) {    setState(() {      param = value;    });  }  @override  Widget build(BuildContext context) {    return Container(      child: widget.builder(param),    );  }}///外部使用 TestWidgetWidget _builder(dynamic param){  if (param == null) {    return Container();  }  switch (param.runtimeType) {    case A:      return AWidget(        a: param as A,      );    case B:      return BWidget(        b: param as B,      );    default:      return Container();  }}return TestWidget(builder: _builder);

方式二:通过继承 + 泛型进行解耦

/// 将 TestWidget 改成抽象类并指定泛型abstract class TestWidget<T> extends StatefulWidget {  const TestWidget({Key? key}) : super(key: key);  Widget childBuilder(T param);  @override  TestWidgetState<T> createState() => TestWidgetState<T>();}class TestWidgetState<T> extends State<TestWidget> {  T? param;  Widget childWidget = Container();  void update(T value) {    setState(() {      param = value;    });  }  @override  Widget build(BuildContext context) {    return Container(      child: widget.childBuilder(param),    );  }}/// 实例Aclass ATestWidget extends TestWidget<A> {  const ATestWidget({Key? key}) : super(key: key);  @override  Widget childBuilder(A param) {    return AWidget(a: param);  }}/// 实例Bclass BTestWidget extends TestWidget<B> {  const BTestWidget({Key? key}) : super(key: key);  @override  Widget childBuilder(B param) {    return BWidget(b: param);  }}

以上就是Dart 多态 控制反转的详细内容