Skip to content

Instantly share code, notes, and snippets.

@orimdominic
Created January 23, 2020 02:37
Show Gist options
  • Save orimdominic/d76ddc2fa5be2f6f9b4b15991b6c280c to your computer and use it in GitHub Desktop.
Save orimdominic/d76ddc2fa5be2f6f9b4b15991b6c280c to your computer and use it in GitHub Desktop.
class Vehicle{
factory Vehicle(int numberOfTyres) {
// An if-else statement can be used too
switch (numberOfTyres) {
case 2:
return Motorcycle();
case 3:
return Tricycle();
case 4:
return Car();
default:
throw Error();
}
}
start(){}
accelerate(){}
stop(){}
}
class Motorcycle implements Vehicle{
// Default Motorcycle constructor
Motorcycle(){
// blah.. An empty constructor will feel lonely so..
print('Motorcycle created');
}
start(){}
accelerate(){}
stop(){}
}
class Tricycle implements Vehicle{
// Default Tricycle constructor
Tricycle(){
// blah.. An empty constructor will feel lonely so..
print('Tricycle created');
}
start(){}
accelerate(){}
stop(){}
}
class Car implements Vehicle{
// Default Car constructor
Car(){
// blah.. An empty constructor will feel lonely so..
print('Car created');
}
start(){}
accelerate(){}
stop(){}
}
void main() {
Vehicle(2);
Vehicle(3);
Vehicle(4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment