Dart中使用单例

class GYDBBaseManager{

  static GYDBBaseManager _instance ;

  static GYDBBaseManager get instance => _getInstance() ;

  factory GYDBBaseManager()=> _getInstance() ;

  GYDBBaseManager._internal(){
    //初始化
  }

  static GYDBBaseManager _getInstance(){
    if(_instance == null){
      _instance = GYDBBaseManager._internal() ;
    }
    return _instance  ;
  }

}
// 无论如何初始化,取到的都是同一个对象
GYDBBaseManager mgr = GYDBBaseManager();
GYDBBaseManager mgr2 = GYDBBaseManager.instance ;

你可能感兴趣的:(Dart中使用单例)