Skip to content

Instantly share code, notes, and snippets.

@JaysonChiang
Created February 26, 2021 03:35
Show Gist options
  • Save JaysonChiang/5c82f3564ddee8fc62d23176957b6798 to your computer and use it in GitHub Desktop.
Save JaysonChiang/5c82f3564ddee8fc62d23176957b6798 to your computer and use it in GitHub Desktop.
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