Suppose we want to write a RPG game which has 3 base character types: Fighter, Mage and Archer. Also we have combined character types like Knight, Paladin and Ranger. In TypeScript we can describe interfaces for characters like this:
// fighter can fight
// mage can cast
// archer can shoot
// paradin can cast and fight
// knight can fight and shoot
// ranger can cast and shoot
type Fighter = {fight: () => string}
type Mage = {cast: () => string}
type Archer = {shoot: () => string}
type Knight = Fighter & Archer
type Paladin = Fighter & Mage
type Ranger = Archer & Mage
The question is how to implement these interfaces using only OOP techniques (imagine that we are working with mainstream OOP language like C#) ?
I had no success expressing this with inheritance without behavior duplication. Currently the only way I see is:
class Paladin {
constructor(private fighter: Fighter, private mage: Mage) {}
fight() {this.fighter.fight()}
cast() {this.mage.cast()}
}
But I don't like that approach because any time I want to create a paladin I also have to create a mage and a fighter which is basically the same person
Aucun commentaire:
Enregistrer un commentaire