简体   繁体   English

颤振/飞镖中工厂方法的目的是什么?

[英]What is the purpose of a factory method in flutter/dart?

I'm following a book that uses a factory method to implement a class that is a singleton.我正在关注一本书,它使用工厂方法来实现一个单例类。

I understand that the main purpose of this is to have just one instance of the class;我知道这样做的主要目的是只有一个类的实例; But what exactly the keyword "factory" does in flutter?但是“工厂”这个关键字究竟在颤振中做了什么?

This is the piece of code that I'm referring:这是我指的一段代码:

static final DbHelper _dbHelper = DbHelper._internal();

DbHelper._internal();

factory DbHelper() => _dbHelper;

I supose that the _dbHelper is the single instance that is being create with the _internal named constructor and that the factory method returns this single instance, is that correct?我假设_dbHelper是使用_internal named constructor创建的单个实例,并且工厂方法返回这个单个实例,对吗? Am I missing something?我错过了什么吗?

You have it correct!你说的对!

You can read more about factory constructors here: https://dart.dev/guides/language/language-tour#factory-constructors您可以在此处阅读有关工厂构造函数的更多信息: https ://dart.dev/guides/language/language-tour#factory-constructors

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

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