Skip to content

Instantly share code, notes, and snippets.

@tom76kimo
Last active September 7, 2018 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom76kimo/b75f64bc738e4d15a4be67f1f858208d to your computer and use it in GitHub Desktop.
Save tom76kimo/b75f64bc738e4d15a4be67f1f858208d to your computer and use it in GitHub Desktop.
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