Created
February 26, 2021 03:35
-
-
Save JaysonChiang/5c82f3564ddee8fc62d23176957b6798 to your computer and use it in GitHub Desktop.
This file contains 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
abstract class Bike { | |
abstract brandname: string; | |
ride() { | |
return `I ride ${this.brandname}`; | |
} | |
} | |
class Giant extends Bike { | |
brandname = 'Giant'; | |
} | |
class Merida extends Bike { | |
brandname = 'Merida'; | |
} | |
class BikeMaker { | |
static factory(brandname: string) { | |
switch (brandname) { | |
case 'giant': | |
return new Giant(); | |
case 'merida': | |
return new Merida(); | |
default: | |
throw { | |
name: 'Error', | |
message: `${brandname} doesn't exist`, | |
}; | |
} | |
} | |
} | |
const mybike = BikeMaker.factory('giant'); | |
const yourbike = BikeMaker.factory('merida'); | |
console.log(mybike.ride()); // This bike is Giant | |
console.log(yourbike.ride()); // This bike is Merida |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment