Dart 工厂构造函数 - 它与“const"构造函数有何不同 [英] Dart factory constructor - how is it different to “const” constructor
问题描述
在 Dart 中,工厂构造函数需要编码人员提供更多逻辑,但与 const 构造函数没有太大区别,只是它们允许非最终"实例变量.
In Dart, factory constructors needs more logic from coders, but not so different from const ones except they permit 'Non final' instance variables.
与 const 构造函数相比,它们的优点是什么?
What are their merits over const constructors?
谢谢大家.
已编辑
以下是 Seth Ladd 的博客Dart - 试图理解工厂"构造函数的价值"中关于工厂构造函数的用法.
Below is about a usage of factory constructor from Seth Ladd's blog ' Dart - Trying to understand the value of 'factory' constructor'.
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'}
}
如上所示,虽然有两个实例,一个 &b、都是不同的对象,效果都是一样的'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); //{something: Instance of 'Symbol'}' as a map object permit only one of same strings as its key.
所以,我的问题是工厂构造函数(或工厂模式)相对于通用/const 构造函数的特殊优点是什么?因为上面的示例代码本身并没有显示出工厂构造函数的优点.
So, my question was what are peculiar merits of factory constructor(or factory pattern) over general/const constructors? Because the above sample code alone shows no merit of factory constructor.
谁能解释一下什么是 Dart 语言中所谓的工厂模式"而不是 Java/C#?
Could anyone explain what is so called 'Factory Pattern' in Dart language rather than Java/C#?
推荐答案
工厂构造函数和 const 构造函数实现完全不同的目的.const 构造函数允许在编译时常量表达式中拥有自定义类的实例.有关 const 构造函数的更多详细信息,请参阅 https://stackoverflow.com/a/21746692/217408.
A factory constructor and a const constructor fulfill entirely different purposes. A const constructor allows to have an instance of a custom class in a compile time constant expression. See https://stackoverflow.com/a/21746692/217408 for more details about the const constructor.
工厂构造函数和返回类的新实例的常量方法更相似.区别在于,工厂构造函数像普通构造函数一样使用 new
调用,并且有一些常量方法没有的限制.
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.
普通构造函数和工厂构造函数之间的主要区别在于,您可以影响是否实际创建了一个新实例以及它是什么具体类型.
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.
在 https://www.dartlang.org/dart-tips/dart-tips-ep-11.html 缓存被认为是一个流行的例子.工厂构造函数可以检查内部缓存中是否有准备好的可重用实例,并返回此实例或以其他方式创建一个新实例.
In https://www.dartlang.org/dart-tips/dart-tips-ep-11.html a cache is mentioned as a popular example. 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.
另一个例子是单例模式.请参阅https://stackoverflow.com/a/12649574/217408了解更多详情.
Another example is the singleton pattern. See https://stackoverflow.com/a/12649574/217408 for more details.
另一个例子是工厂模式.例如,您可以拥有一个抽象类 A
(无法实例化),其中包含一个工厂构造函数,该构造函数返回 A
的具体子类的实例,具体取决于例如传递给工厂构造函数的参数.
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.
这里有一个类似的问题 Dart - 试图理解工厂"构造函数的值
Here is a similar question Dart - Trying to understand the value of 'factory' constructor
这篇关于Dart 工厂构造函数 - 它与“const"构造函数有何不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!