素钗一生推 2019-06-28
class Manager { // 工厂模式 factory Manager() =>_getInstance() static Manager get instance => _getInstance(); static Manager _instance; Manager._internal() { // 初始化 } static Manager _getInstance() { if (_instance == null) { _instance = new Manager._internal(); } return _instance; } }
// 无论如何初始化,取到的都是同一个对象 Manager manager = new Manager(); Manager manager2 = Manager.instance;