If I am understanding correctly:
\"A factory constructor affords an abstract class to be
instantiated by another class, despite being abstract.\"
>
I don't think that the problem in the factory
.
Your code initially was wrong.
Look at this code and make your conclusions.
Locomotive locomotive = new SomethingWithSmokeFromChimney();
Now look at this code.
Plant plant = new SomethingWithSmokeFromChimney();
You mistakenly believe that all animals on the earth (even the dogs) are cats.
Cat cat = new Animal();
If you want this.
Cat cat = new Animal();
Dog dog = new Animal();
Then (if I correctly understand you) you also want this.
// Cat cat = new Animal();
// Dog dog = new Animal();
Dog dog = new Cat();
P.S.
The same wrong conclusions but without factory
.
void main() {
Cat cat = new Animal();
Dog dog = new Animal();
}
class Animal {
}
class Cat implements Animal {
}
class Dog implements Animal {
}
But this code (depending on documenation) may be considered correct.
void main() {
Cat cat = new Animal("cat");
Dog dog = new Animal("dog");
}
abstract class Animal {
factory Animal(String type) {
switch(type) {
case "cat":
return new Cat();
case "dog":
return new Dog();
default:
throw "The '$type' is not an animal";
}
}
}
class Cat implements Animal {
}
class Dog implements Animal {
}
The factory constructor of the abstract class can return (by default) some default implementation of this abstract class.
abstract class Future {
factory Future(computation()) {
_Future result = new _Future();
Timer.run(() {
try {
result._complete(computation());
} catch (e, s) {
result._completeError(e, s);
}
});
return result;
}
}