简体   繁体   English

Dart工厂构造函数 - 它与“const”构造函数有什么不同

[英]Dart factory constructor - how is it different to “const” constructor

In Dart, factory constructors needs more logic from coders, but not so different from const ones except they permit 'Non final' instance variables. 在Dart中,工厂构造函数需要来自编码器的更多逻辑,但与const类型不同,除非它们允许“非最终”实例变量。

What are their merits over const constructors? 它们对const构造函数的优点是什么?

Thank you all. 谢谢你们。

Edited 编辑

Below is about a usage of factory constructor from Seth Ladd's blog ' Dart - Trying to understand the value of 'factory' constructor'. 下面是关于Seth Ladd的博客'Dart - 试图了解'工厂'构造函数'的价值的工厂构造函数的用法。

class Symbol {
  final String name;
  static Map<String, Symbol> _cache = new Map<String, Symbol>();

  factory Symbol(String name) {
    if (_cache.containsKey(name)) {
      return _cache[name];
    } else {
      final symbol = new Symbol._internal(name);
      _cache[name] = symbol;
      return symbol;
    }
  }

  Symbol._internal(this.name);
}


main() {
  var x = new Symbol('X');
  var alsoX = new Symbol('X');

  print(identical(x, alsoX));  // true
}

IMHO, with general constructor, the same effect can be achieved with a subtle difference, but quite simpler. 恕我直言,与一般的构造函数,可以实现相同的效果与微妙的差异,但相当简单。

class Symbol {
  static final Map<String, Symbol> cache = {};
  final String name;

  Symbol(name) {
    cache[name] = new Symbol._internal();
  }

  Symbol._internal();
}

main(){
var a = new Symbol('something');
var b = new Symbol('something');

print(identical(a, b)); // false!
print(Symbol.cache); //{something: Instance of 'Symbol'}
}

As shown above, though the two instances, a & b, are different objects, the effect is all the same as shown in 'print(Symbol.cache); 如上所示,虽然两个实例a和b是不同的对象,但效果与'print(Symbol.cache); //{something: Instance of 'Symbol'}' as a map object permit only one of same strings as its key. // {something:'Symbol'的实例}作为一个地图对象,只允许其中一个相同的字符串作为其键。

So, my question was what are peculiar merits of factory constructor(or factory pattern) over general/const constructors? 所以,我的问题是工厂构造函数(或工厂模式)对一般/ const构造函数的特殊优点是什么? Because the above sample code alone shows no merit of factory constructor. 因为上面的示例代码没有显示工厂构造函数的优点。

Could anyone explain what is so called 'Factory Pattern' in Dart language rather than Java/C#? 任何人都可以解释Dart语言中所谓的“工厂模式”而不是Java / C#吗?

A factory constructor and a const constructor fulfill entirely different purposes. 工厂构造函数和const构造函数实现完全不同的目的。 A const constructor allows to have an instance of a custom class in a compile time constant expression. const构造函数允许在编译时常量表达式中具有自定义类的实例。 See https://stackoverflow.com/a/21746692/217408 for more details about the const constructor. 有关const构造函数的更多详细信息,请参见https://stackoverflow.com/a/21746692/217408

A factory constructor and a constant method that returns a new instance of a class are more similar. 工厂构造函数和返回类的新实例的常量方法更相似。 The difference is, that a factory constructor is called with new like a normal constructor and has some limitations a constant method doesn't have. 不同之处在于,工厂构造函数与普通构造函数一样使用new调用,并且具有常量方法所没有的一些限制。

The main difference between a normal constructor and a factory constructor is, that you can influence if actually a new instance is created and of what concrete type it is. 普通构造函数和工厂构造函数之间的主要区别在于,您可以影响实际创建新实例以及它的具体类型。

In https://www.dartlang.org/dart-tips/dart-tips-ep-11.html a cache is mentioned as a popular example. https://www.dartlang.org/dart-tips/dart-tips-ep-11.html中,提到缓存是一个流行的例子。 A factory constructor can check if it has a prepared reusable instance in an internal cache and return this instance or otherwise create a new one. 工厂构造函数可以检查它是否在内部缓存中具有准备好的可重用实例并返回此实例或以其他方式创建新实例。

Another example is the singleton pattern. 另一个例子是单身模式。 See https://stackoverflow.com/a/12649574/217408 for more details. 有关详细信息,请参阅https://stackoverflow.com/a/12649574/217408

Another example is the factory pattern. 另一个例子是工厂模式。 You can for example have an abstract class A (which can't be instantiated) with a factory constructor that returns an instance of a concrete subclass of A depending for example on the arguments passed to the factory constructor. 例如,您可以使用工厂构造函数来获取抽象类A (无法实例化),该构造函数返回A的具体子类的实例,具体取决于传递给工厂构造函数的参数。

Here is a similar question Dart - Trying to understand the value of 'factory' constructor 这是一个类似的问题Dart - 试图理解'factory'构造函数的价值

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

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