[Solved] how to clearly design these two class? [closed]


It’s a question of design. Weapon has the field addPower, but IWeapon does not. This would imply, that not all weapons (not all classes that implement IWeapon) do add power to the character. So you can’t use this property, if the interface doesn’t define it. (Casting an interface to an implementing class defies the whole point of using an interface in the first place)

If all weapons can add power, then the interface IWeapon needs to reflect this requirement of a weapon:

interface IWeapon {
    void OnEquip(ILife life);

    int AddPower { get; }
}

Then all weapons need to implement this property:

class Weapon : IWeapon {
    int addPower=200;

    public int AddPower 
    {
        get {
            return this.addPower;
        }
    }
}

If a weapon does not add any power (e.g. defensive equipment) it might as well return 0 in AddPower, makes no difference.

1

solved how to clearly design these two class? [closed]