Created
January 23, 2020 02:37
-
-
Save orimdominic/d76ddc2fa5be2f6f9b4b15991b6c280c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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