如何用Dart写一个单例
由于Dart拥有factory constructors,因此构建单例模式很容易。
class Singleton {
static final Singleton _singleton = new Singleton._internal();
factory Singleton() {
return _singleton;
}
Singleton._internal();
}我们可以使用new来构造代码如下:
main() {
var s1 = new Singleton();
var s2 = new Singleton();
print(identical(s1, s2)); // true
print(s1 == s2); // true
} 相关推荐
万物weiyi 2020-06-16
Samlss 2020-06-04
liutong 2020-05-12
万物weiyi 2020-03-04
mryangjx 2020-03-01
liutong 2020-02-26
liutong 2020-02-22
Samlss 2020-02-22
chaoxiao 2020-02-15
mryangjx 2020-01-28
mryangjx 2020-01-25
liutong 2020-01-12
apowerfulman 2020-01-07
Samlss 2020-01-07
万物weiyi 2020-01-01
mryangjx 2019-12-25
Samlss 2019-12-15