Skip to content

Instantly share code, notes, and snippets.

@theScottyJam
Last active November 10, 2021 15:14
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 theScottyJam/2cbef2a4ac6a8c61382a6b5beeb3d65a to your computer and use it in GitHub Desktop.
Save theScottyJam/2cbef2a4ac6a8c61382a6b5beeb3d65a to your computer and use it in GitHub Desktop.
Article: Composition Alone Can't Replace Inheritance - classic composition
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