工厂函数

调用子类

abstract class Phone {
  void call();
  factory Phone(String type) {
    switch (type) {
      case "android":
        return Android();
      case "ios":
        return Ios();
      default:
        throw "The '$type' is not an animal";
    }
  }
}

class Android implements Phone {
  
  void call() {
    print('Android Calling...');
  }
}

class Ios implements Phone {
  
  void call() {
    print('Ios Calling...');
  }
}

void main() {
  var android = Phone('android');
  var ios = Phone('ios');
  android.call();
  ios.call();
}

单例模式

class Phone {
  static final Phone _single = Phone._internal();
  Phone._internal();

  factory Phone() {
    return _single;
  }

  void call() {
    print('Calling...');
  }
}

void main() {
  var p1 = Phone();
  var p2 = Phone();
  print(identical(p1, p2));

  Phone().call();
}

减少重复实例对象

class Phone {
  int _number;
  Phone(this._number);

  factory Phone.fromJson(Map<String, dynamic> json) =>
      Phone(json['number'] as int);

  void call() {
    print('Calling $_number...');
  }
}

void main() {
  var p = Phone.fromJson({"number": 911});
  p.call();
}

如果不用工厂函数,就要用类静态方法,这样会有多余的实例对象

Last Updated:
Contributors: ducafecat