本文介绍了Dart工厂构造函数 - 它是如何不同于“const”构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dart中,工厂构造函数需要更多的编译器逻辑,但是与const不同,除非它们允许非final实例变量。



over const构造函数?



谢谢大家。



已编辑

下面是关于Seth Ladd的博客Dart - 试图理解'factory'constructor'的工厂构造函数的使用。

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

factory符号(字符串名){
if(_cache.containsKey(name)){
return _cache [name];
} else {
final symbol = new Symbol._internal(name);
_cache [name] = symbol;
返回符号;
}
}

Symbol._internal(this.name);
}


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

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

IMHO使用通用构造函数,可以实现相同的效果一个微妙的差别,但相当简单。

  class Symbol {
static final Map final String 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:'Symbol'}的实例作为一个map对象只允许使用与其键相同的字符串。



所以,我的问题是什么是特殊的优点的工厂构造函数(或工厂模式)一般/ const构造函数?因为上面的示例代码没有显示出工厂构造函数的优点。



有没有人能解释Dart语言而不是Java / C#中所谓的Factory Pattern?

解决方案

工厂构造函数和const构造函数完全不同的目的。 const构造函数允许在编译时常量表达式中有一个自定义类的实例。有关const构造函数的更多详细信息,请参见。



工厂构造函数和返回类的新实例的常量方法更类似。不同之处在于,工厂构造函数以 new 的方式调用,像一个普通的构造函数,并且有一些常量方法没有的限制。



正常构造函数和工厂构造函数之间的主要区别是,你可以影响实际创建一个新实例以及它是什么具体类型。



在一个缓存被提及作为一个流行的例子。工厂构造函数可以检查它是否在内部缓存中有一个准备好的可重用实例,并返回这个实例或创建一个新的实例。



另一个例子是单例模式。有关详情,请参见。



另一个例子是工厂模式。
例如,您可以使用工厂构造函数来创建一个抽象类 A (不能实例化),该工厂构造函数返回<$ c



这是一个类似的问题


In Dart, factory constructors needs more logic from coders, but not so different from const ones except they permit 'Non final' instance variables.

What are their merits over const constructors?

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'.

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); //{something: Instance of 'Symbol'}' as a map object permit only one of same strings as its key.

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.

Could anyone explain what is so called 'Factory Pattern' in Dart language rather than Java/C#?

解决方案

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 http://stackoverflow.com/a/21746692/217408 for more details about the const constructor.

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.

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.

Another example is the singleton pattern. See http://stackoverflow.com/a/12649574/217408 for more details.

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.

Here is a similar question Dart - Trying to understand the value of 'factory' constructor

这篇关于Dart工厂构造函数 - 它是如何不同于“const”构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:33