美文网首页
Flutter MVC的另一种思路是否可行?

Flutter MVC的另一种思路是否可行?

作者: C_G__ | 来源:发表于2019-08-04 10:35 被阅读0次

MVC模式,不做解释。

class M {
  request() {}
}

class V {
  Function afunc;
  onTap() {
    afunc();
  }
}

class C {
  test() {
    V v = V();
    M m = M();
    v.afunc = () {
      m.request();
    };
  }
}

扩展一下

abstract class IPage {
  Map makeParams();
  bool checkParams();
  success();
  failure();
}

abstract class ILoginPage extends IPage {
  Function loginFunc;
  Function gotoFunc;
}

class LoginPageImpl implements ILoginPage {

  @override
  Function loginFunc;
  @override
  Function gotoFunc;

  onTap1() {
    loginFunc();
  }

  onTap2() {
    gotoFunc();
  }

  @override
  Map makeParams() {
    return null;
  }

  @override
  bool checkParams() {
    return true;
  }

  @override
  success() {}
  @override
  failure() {}
}

abstract class ILoginModel {
  bool requestLogin(Map params, Function callback);
}

class LoginModelImpl implements ILoginModel {
  @override
  bool requestLogin(Map params, Function callback) {}
}

class Controller {

  test() {
    ILoginModel model = new LoginModelImpl();
    ILoginPage page = new LoginPageImpl();

    page.gotoFunc = () {
      Navigator.push(null, null);
    };

    page.loginFunc = () {
      Map params = page.makeParams();
      if (page.checkParams()) {
        model.requestLogin(params, (resJosn) {
          if (resJosn == true) {
            page.success();
          } else {
            page.failure();
          }
        });
      }
    };
  }

}

以上仅供参考。
注意事项:

  1. page中,Function对象(f1, f2 )的参数类型和个数不能固定,多人开发时需定义好格式。
  2. 性能未测。
  3. 由于刚学习flutter一周时间,不知道此思路是否可行,仅供参考,欢迎大家讨论指正。

相关文章

网友评论

      本文标题:Flutter MVC的另一种思路是否可行?

      本文链接:https://www.haomeiwen.com/subject/pmpedctx.html