理解工厂构造函数代码示例 - Dart [英] Understanding Factory constructor code example - Dart

查看:40
本文介绍了理解工厂构造函数代码示例 - Dart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这里提到的工厂构造函数示例有一些琐碎的问题(

I have some niggling questions about factory constructors example mentioned here (https://www.dartlang.org/guides/language/language-tour#factory-constructors). I am aware of only three types of constructors on a basic level - default, named and parameterised.

推荐答案

tl;dr 在您必然不想返回 new 类本身的实例.用例:

解决方案
  • 构造函数很昂贵,所以你想返回一个现有的实例——如果可能的话——而不是创建一个新的实例;
  • 你只想创建一个类的一个实例(单例模式);
  • 您想返回一个子类实例而不是类本身.

说明

Dart 类可能具有生成构造函数工厂构造函数.生成构造函数是一个始终返回该类的新实例的函数.因此,它不使用 return 关键字.常见的生成构造函数的形式如下:

Explanation

A Dart class may have generative constructors or factory constructors. A generative constructor is a function that always returns a new instance of the class. Because of this, it does not utilize the return keyword. A common generative constructor is of the form: 

工厂构造函数比生成构造函数具有更宽松的约束.工厂只需要返回与类相同类型或实现其方法(即满足其接口)的实例.这可能是类的新实例,但也可以是类的现有实例或子类的新/现有实例(它们必须具有与父类相同的方法).工厂可以使用控制流来确定要返回的对象,并且必须使用 return 关键字.为了让工厂返回一个新的类实例,它必须首先调用一个生成构造函数.

class Person { String name; String country; // unnamed generative constructor Person(this.name, this.country); } var p = Person("...") // returns a new instance of the Person class

在您的示例中,未命名的工厂构造函数首先从名为 _cache 的 Map 属性读取(由于它是 Static,因此存储在类级别,因此是独立的任何实例变量).如果实例变量已经存在,则返回它.否则,通过调用命名的生成构造函数 Logger._internal 生成一个新实例.该值被缓存然后返回.由于生成构造函数只接受一个 name 参数,mute 属性将始终被初始化为 false,但可以使用默认设置器进行更改:

In your example, the unnamed factory constructor first reads from a Map property called _cache (which, because it is Static, is stored at the class level and therefore independent of any instance variable). If an instance variable already exists, it is returned. Otherwise, a new instance is generated by calling the named generative constructor Logger._internal. This value is cached and then returned. Because the generative constructor takes only a name parameter, the mute property will always be initialized to false, but can be changed with the default setter:

术语factory 暗指工厂模式,即允许构造函数根据提供的参数返回子类实例(而不是类实例).Dart 中这个用例的一个很好的例子是抽象的 HTML 元素类,定义了几十个命名工厂构造函数,返回不同的子类.例如,Element.div()Element.li() 返回

var log = Logger("..."); log.mute = true; log.log(...); // will not print

  • code> 元素,分别.
  • The term factory alludes to the Factory Pattern, which is all about allowing a constructor to return a subclass instance (instead of a class instance) based on the arguments supplied. A good example of this use case in Dart is the abstract HTML Element class, which defines dozens of named factory constructor functions returning different subclasses. For example, Element.div() and Element.li() return <div> and <li> elements, respectively.

    在这个缓存应用程序中,我发现工厂"有点用词不当,因为它的目的是避免调用生成构造函数,而且我认为现实世界的工厂本质上是生成的.也许这里更合适的术语是仓库":如果某件物品已经可用,则将其从货架上取下并交付.如果没有,请换一个新的.

    In this caching application, I find "factory" a bit of a misnomer since its purpose is to avoid calls to the generative constructor, and I think of real-world factories as inherently generative. Perhaps a more suitable term here would be "warehouse": if an item is already available, pull it off the shelf and deliver it. If not, call for a new one.

    所有这些与命名构造函数有什么关系?生成式构造函数和工厂构造函数都可以是未命名的或命名的:

    How does all this relate to named constructors? Generative and factory constructors can both be either unnamed or named:

    ...
      // named generative
      // delegates to the default generative constructor
      Person.greek(String name) : this(name, "Greece"); 
    
      // named factory 
      factory Person.greek(String name) {
        return Greek(name);
      }
    }
    
    class Greek extends Person {
      Greek(String name) : super(name, "Greece");
    }
    
    
    

    这篇关于理解工厂构造函数代码示例 - Dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆