Created
May 3, 2020 11:22
-
-
Save armanozak/c8591c5d47ea270c34e4a4dabbffc815 to your computer and use it in GitHub Desktop.
[Strategy Pattern with TypeScript] Before Implementation #blog #typescript
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 Hero { | |
constructor(public name: string, public weapon?: string) {} | |
} | |
class Avengers { | |
private ensemble: Hero[] = []; | |
private blast(hero: Hero) { | |
console.log(`${hero.name} blasted ${hero.weapon}`); | |
} | |
private kickAndPunch(hero: Hero) { | |
console.log(`${hero.name} kicked and punched`); | |
} | |
private shoot(hero: Hero) { | |
console.log(`${hero.name} shot ${hero.weapon}`); | |
} | |
private throw(hero: Hero) { | |
console.log(`${hero.name} threw ${hero.weapon}`); | |
} | |
recruit(hero: Hero) { | |
this.ensemble = this.ensemble | |
.filter(({name}) => name === hero.name) | |
.concat(hero); | |
} | |
fight() { | |
this.ensemble.forEach(hero => this.attack(hero)); | |
} | |
attack(hero: Hero) { | |
switch(hero.name) { | |
case 'Iron Man': | |
this.blast(hero); | |
break; | |
case 'Captain America': | |
case 'Thor': | |
this.throw(hero); | |
break; | |
case 'The Hulk': | |
this.kickAndPunch(hero); | |
break; | |
case 'Black Widow': | |
hero.weapon ? this.shoot(hero) : this.kickAndPunch(hero); | |
break; | |
case 'Hawkeye': | |
this.shoot(hero); | |
break; | |
default: | |
console.warn('Unknown Avenger: ' + hero.name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment