单例模式确保只创建一个类的实例。我如何在达特建立这个?
当前回答
从Dart 2.13版本开始,就很容易用late关键字了。Late关键字允许我们延迟实例化对象。
作为一个例子,你可以看到:
class LazySingletonExample {
LazySingletonExample._() {
print('instance created.');
}
static late final LazySingletonExample instance = LazySingletonExample._();
}
注意:请记住,当你调用lazy instance field时,它只会被实例化一次。
其他回答
从Dart 2.13版本开始,就很容易用late关键字了。Late关键字允许我们延迟实例化对象。
作为一个例子,你可以看到:
class LazySingletonExample {
LazySingletonExample._() {
print('instance created.');
}
static late final LazySingletonExample instance = LazySingletonExample._();
}
注意:请记住,当你调用lazy instance field时,它只会被实例化一次。
因为我不太喜欢使用new关键字或其他构造函数,比如对单例对象的调用,我更喜欢使用一个名为inst的静态getter:
// the singleton class
class Dao {
// singleton boilerplate
Dao._internal() {}
static final Dao _singleton = new Dao._internal();
static get inst => _singleton;
// business logic
void greet() => print("Hello from singleton");
}
使用示例:
Dao.inst.greet(); // call a method
// Dao x = new Dao(); // compiler error: Method not found: 'Dao'
// verify that there only exists one and only one instance
assert(identical(Dao.inst, Dao.inst));
使用空安全操作符和工厂构造函数可以更好地创建单例对象。
class Singleton {
static Singleton? _instance;
Singleton._internal();
factory Singleton() => _instance ??= Singleton._internal();
void someMethod() {
print("someMethod Called");
}
}
用法:
void main() {
Singleton object = Singleton();
object.someMethod(); /// Output: someMethod Called
}
注意:? ?是一个Null感知操作符,如果左边值为Null,它将返回右边的值,这意味着在我们的例子中_instance ??Singleton._internal();, Singleton._internal()将在第一次调用object时返回,rest _instance将返回。
由const构造函数和工厂组成的Dart单例
class Singleton {
factory Singleton() =>
Singleton._internal_();
Singleton._internal_();
}
void main() {
print(new Singleton() == new Singleton());
print(identical(new Singleton() , new Singleton()));
}
在Dart中创建Singleton没有什么棘手的。您可以在顶级(全局)位置声明任何变量,默认情况下这是一个单例。也可以将变量声明为类的静态成员。这是单元素a。
class A {}
final a = A();
However, the above does not allow you to replace the instance for testing. The other issue is that as the app grows in complexity, you may want to convert global or static variables to transient dependencies inside your classes. If you use dependency injection, you can change a dependency inside your composition at any time. This is an example of using ioc_container to configure a singleton instance of A in the root of an app. You can change this to a transient dependency any time by using add instead of addSingletonService
import 'package:ioc_container/ioc_container.dart';
class A {}
void main(List<String> arguments) {
final builder = IocContainerBuilder()..addSingletonService(A());
final container = builder.toContainer();
final a1 = container<A>();
final a2 = container<A>();
print(identical(a1, a2));
}
上面打印为真,因为应用程序只会生成一个A实例。