简体   繁体   English

如何在Dart中的类上调度构造函数?

[英]How do I dispatch a constructor on a class in Dart?

I previously had the following code, it works fine. 我以前有以下代码,它工作正常。 (note that Card, SearchResults, Quiz all extend Persistable, and Persistable contains the constructor .fromMap . (注意,Card,SearchResults,Quiz都扩展为Persistable,而Persistable包含构造函数.fromMap

Persistable fromString(String value){
  Map<String, dynamic> m = parse(value);

  switch(m['type']){
    case 'card':
      return new Card.fromMap(m);
    case 'searchresults':
      return new SearchResults.fromMap(m);
    case 'quiz':
      return new Quiz.fromMap(m);
  }
}

It was a bit wordy, so I thought I would break it down into two parts. 这有点罗嗦,所以我想我会把它分成两部分。 I first have this: 我先这个:

static final Map<String, Persistable> lookup =
    {'card': Card,
     'searchresults': SearchResults,
     'quiz': Quiz };

Seems reasonable, but then when I try to redefine the method, I get confused. 看似合理,但是当我尝试重新定义方法时,我感到困惑。

Persistable fromString(String value){
  Map<String, dynamic> m = parse(value);
  String type = m['type'];
  Persistable p = lookup[type];
  ... Confused, this can't be right
  ... ultimately want to "return new p.fromMap(m)";
}

Persistable p really means a instance of class Persistable . Persistable p实际上意味着类Persistable的实例。 How do I type my lookup map so that its values are of the class Persistable , so that I can call their .fromMap constructors? 如何键入我的lookup映射,使其值为Persistable类,以便我可以调用它们的.fromMap构造函数?

First of all I think your initial approach is perfectly valid and should not be cast away owing simply to its verbosity. 首先,我认为你最初的方法是完全有效的,不应该因为它的冗长而被丢弃。 I believe alternative approaches introduce additional complexity and are justified only if you are really in need of dynamic dispatch. 我相信替代方法会带来额外的复杂性,只有在您真正需要动态调度时才有道理。 (For example if you write library for persistency and you wish to add ability to register arbitrary class for persistency for clients of library) If dynamic dispatch is a must for you I believe there is two main possibility: - Reflection API. (例如,如果您为了持久性而编写库,并且希望添加为库的客户端注册任意类以保持持久性的能力)如果动态调度是您必须的,我相信有两种主要可能性: - Reflection API。 Recently reflection library got sync API, so this way is now much more affordable then before. 最近反射库得到了同步API,所以这种方式现在比以前更实惠。 I believe there always will be some cost incurred by reflection anyway. 我相信无论如何反思总会产生一些成本。 - Use core DART functionality. - 使用核心DART功能。

With the second approach you may use some sort of trick to imitate constructor call dynamically. 使用第二种方法,您可以使用某种技巧来动态模仿构造函数调用。 For instance you may store in map not Type variable but function which returns instance of required class: 例如,您可以在map中存储而不是Type变量,而是返回所需类的实例的函数:

So your code may look something like 所以你的代码可能看起来像

static final Map<String, Function> lookup = new Map<String, Function>
static void registerClass(String className, factory) {
    lookup[className] = factory;
}
static Persistable getInstance(String className, Map map){
    return lookup[className](map);
}

And on client side: 在客户端:

....
registerClass('quiz', (map)=> new Quiz.fromMap(map));
registerClass('card', (map)=> new Card.fromMap(map));

(Attention - I did not test this) You may look for working sample code for that approach in https://github.com/vadimtsushko/objectory (注意 - 我没有对此进行测试)您可以在https://github.com/vadimtsushko/objectory中查找该方法的工作示例代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM