Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.