
The instance creation expressions are based on interfaces and minimizes the need fordependency injection.
Example of use of factory:
interface Person factory PersonFactory{
Person(name);
final name;
}
class PersonFactory{
factory Person(name){
if(name== null) return const Ghost();
return new RealPerson(name);
}
}
class RealPerson implements Person{
RealPerson(this.name);
final name;
}
class Ghost implements Person{
const Ghost();
get name() => "ghost";
}
main(){
print(new Person("Pepita") is RealPerson);
print(new Person(null) is Ghost);
}




Español


Recent Comments