Last active
November 10, 2021 15:14
-
-
Save theScottyJam/2cbef2a4ac6a8c61382a6b5beeb3d65a to your computer and use it in GitHub Desktop.
Article: Composition Alone Can't Replace Inheritance - classic composition
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
class MonsterBehaviors { | |
#dropLoot | |
constructor({ dropLoot }) { | |
this.#dropLoot = dropLoot | |
} | |
attack() { | |
playHurtAnimation() | |
} | |
die() { | |
userInventory.add(this.#dropLoot()) | |
removeFromUi(this) | |
} | |
} | |
export class Slime { | |
#monsterBehaviors | |
constructor() { | |
this.#monsterBehaviors = new MonsterBehaviors({ | |
dropLoot: () => this.dropLoot(), | |
}) | |
} | |
dropLoot() { | |
return [new SlimeBall()] | |
} | |
attack() { | |
this.#monsterBehaviors.attack() | |
} | |
die() { | |
return this.#monsterBehaviors.die() | |
} | |
} | |
export class Skeleton { ... } | |
export class Clown { ... } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment