在 dart 中实例化泛型类



我已经使用 typedef 查看了堆栈溢出的示例,但看起来它主要用于回调,所以不确定它是否与我正在做的事情相关。我正在使用执行 RPC 的泛型实现一个类......

abstract class Message {
  int created = new DateTime.now().millisecondsSinceEpoch;
  Map map = new Map();
  Map toJson();
  void fromJson(String json){
    map = JSON.decode(json);
    this.created = map["created"];
  }
  String toString() {
    return JSON.encode(this);
  }
  Message(){
    map["created"] = created;
  }
}

___Request和___Response都扩展了消息:

import 'Message.dart';
class TestResponse extends Message {
  String test;
  String message;
  Map toJson() {
    map["test"] = this.test;
    return map;
  }
  fromJson(String json) {
    super.fromJson(json);
    this.test = map["test"];
    this.message = map["message"];
  }
}

现在,当我尝试执行一个隐藏发送和接收消息的所有样板的通用 RPC 类时,我需要创建响应类的新实例以将其发回。(我本来更喜欢做 RPC.submit,但这给了我一个错误,说静态静态成员不能引用类型参数,所以我的另一个选择是可能滥用构造函数语法,例如 RPC.submit(json, uri).getResponse() ...

import 'package:http/browser_client.dart';
import 'Message.dart';
class RPC<REQ extends Message, RES extends Message> {
  RES submit(REQ req, String uri){
    var client = new BrowserClient();
    var url = "http://localhost:9090/application-api" + uri;
    RES res = new RES(); // <----- can't do this
    client.post(url, body: req.toString()).then((response){
      print("Response status: ${response.statusCode}");
      print("Response body: ${response.body}");
      res.fromJson(response.body);
    });
    return res;  
  }
}

在我的提交方法中,我显然可以传入"RES res"的实例并使用它,但我希望它可以在通用 RPC 中完成,而无需太多额外的样板,这在 dart 中可能吗?

似乎

与 http://dartbug.com/10667 有关

在类似情况下,我所做的是创建一个静态映射,将类型映射到封闭的构造函数。我使用消息类型和每个消息类型的闭包初始化映射,从而创建该类型的新实例。然后我使用 type 参数查找闭包并调用返回的闭包以获取一个新实例。

var factories = {
  'A': () => new A(),
  'B': () => new B(),
  'C': () => new C(),
};
...
var a = factories['A']();

您可以将工厂集成到类中

class A {
  static A createNew() => new A();
}
var factories = {
  'A': A.createNew,
  'B': B.createNew,
  'C': C.createNew,
};
...
var a = factories['A']();

您不能使用在需要时生成响应并将其传递给 RPC 类的自定义工厂吗?这对我来说似乎很简单:

class RPC<REQ extends Message, RES extends Message> {
  static MessageFactory factory;
  RES submit(REQ req, String uri){
    // ...
    RES res = factory.createRES();
    // ..  
  }
}
abstract class MessageFactory {
   RES createRES();
}
class TestFactory extends MessageFactory {
  RES createRES() {
    return new TestResponse();
  }
}

代码中的某个地方

RPC.factory = new TestFactory();

最新更新