Last active
September 7, 2018 08:59
-
-
Save tom76kimo/b75f64bc738e4d15a4be67f1f858208d 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
interface Minion { | |
behit(damage: number): void; | |
getBlood(): number; | |
} | |
class SilverHandRecruit implements Minion { | |
private blood: number = 10; | |
behit(damage: number) { | |
this.blood -= damage; | |
} | |
getBlood() { | |
return this.blood; | |
} | |
} | |
class DivineShield implements Minion { | |
private minion: Minion; | |
private shieldOn: boolean = true; | |
constructor(minion: Minion) { | |
this.minion = minion; | |
} | |
behit(damage: number) { | |
if (this.shieldOn) { | |
this.minion.behit(0); | |
this.shieldOn = false; | |
} else { | |
this.minion.behit(damage); | |
} | |
} | |
getBlood() { | |
return this.minion.getBlood(); | |
} | |
} | |
const minion: Minion = new SilverHandRecruit(); | |
const minionWithDivineShield: Minion = new DivineShield(minion); | |
console.log(minionWithDivineShield.getBlood()); | |
console.log('===be hit by 5'); | |
minionWithDivineShield.behit(5); | |
console.log(minionWithDivineShield.getBlood()); | |
console.log('===be hit by 5'); | |
minionWithDivineShield.behit(5); | |
console.log(minionWithDivineShield.getBlood()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment