class Person { final String name; Person(this.name); Person.copy(Person original) : this.name = original.name; } var hara = new Person("Hara"); var hara2 = new Person.copy(hara);
定、DIコンテナへの委譲など Factory Constructor abstract class Person { final int age; Person._internal(this.age); factory Person(int age) => age < 20 ? new Young(age) : new Adult(age); } class Young extends Person { Young(int age) : super._internal(age); } class Adult extends Person { Adult(int age) : super._internal(age); }
Person { final bool isAdult; Person(int age) : this.isAdult = age < 20; } class Young implements Person { // getterのオーバーライド bool get isAdult => false; }
▪ super呼び出しがされていないこと class Printable { void printMe() => print(this.toString()); } class Person extends Object with Printable { final int age; Person(this.age); String toString() => "I'm $age years old."; }
Pattern class Repository { List<String> find() => ["data1", "data2"]; } class Service { Repository get repository => new Repository(); void execute() => print(repository.find()); } class AwesomeRepository implements Repository { List<String> find() => ["awesomeData1", "awesomeData2"]; } abstract class AwesomeRepositoryModule { Repository get repository => new AwesomeRepository(); } typedef AwesomeService = Service with AwesomeRepositoryModule;