zoukankan      html  css  js  c++  java
  • dart基础---->单例singleton

    At least, there are three ways to create the singleton object with dart.

    1. factory constructor

    class SingletonOne {
      SingletonOne._privateConstructor();
      static final SingletonOne _instance = SingletonOne._privateConstructor();
    
      factory SingletonOne() {
        return _instance;
      }
    }
    

    2. static field with getter

    class SingletonTwo {
      SingletonTwo._privateConstructor();
      static final SingletonTwo _instance = SingletonTwo._privateConstructor();
    
      static SingletonTwo get instance => _instance;
    }
    

    3. static field

    class SingletonThree {
      SingletonThree._privateConstructor();
    
      static final SingletonThree instance = SingletonThree._privateConstructor();
    }
    

    How to instanstiate

    The above singletons are instantiated like this:

    SingletonOne one = SingletonOne();
    SingletonTwo two = SingletonTwo.instance;
    SingletonThree three = SingletonThree.instance;
    

    Example to test it

    main(List<String> args) {
      var s1 = SingletonOne();
      var s2 = SingletonOne();
      print(identical(s1, s2)); // true
      print(s1 == s2); // true
    }
    

  • 相关阅读:
    Linux系统配置静态ip
    爬虫之如何找js入口(一)
    asyncio动态添加任务
    关于python导包问题
    python动态添加属性
    requests模块
    反selenium关键字
    PIL模块
    openxlsx模块
    CSV
  • 原文地址:https://www.cnblogs.com/huhx/p/13364314.html
Copyright © 2011-2022 走看看