Liskov Substitution Principle with multiple inheritance heirachies

拈花ヽ惹草 提交于 2019-12-25 01:30:43

问题


I am trying to come up with an object oriented design, and having difficulty satisfying the Liskov Substitution Principle. Here is an illustrative example:

class Food
{
    public:
    virtual void printName() {
    //......
    }
};
class Fruit : public Food
{
};
class Meat : public Food
{
};
class Animal
{
    public:
    Food *_preferredFood;
    virtual void setFoodPreference(Food *food)=0;

};
class Carnivore: public Animal
{
    public:
    void setFoodPreference(Food *food) {
        this->_preferredFood = dynamic_cast<Meat *>(food);
    }
};
class Herbivore: public Animal
{
    public:
    void setFoodPreference(Food *food) {
        this->_preferredFood = dynamic_cast<Fruit *>(food);
    }
};

How can I enforce the following:

  1. Each subclass of Animal should allow setting a food preference, without breaking LSP
  2. The food preference for each derived class of animal is a subclass of Food

Fore example, if somebody extends Animal to create MarineMammal, the food preference might be Fish (which they will create by extending Food).


回答1:


When Carnivore::setFoodPreference only accepts Meat and Herbivore::setFoodPreference only accepts Fruit, then they do not comply to the same contract. That means they are not actually the same method. When you call this method, you have to know if you are dealing with a carnivore or a herbivore to avoid passing the wrong type. When you forget to check this, you risk creating a bug which manifests in form of a casting error at runtime.

The solution is to separate these two methods.

I would recommend you to remove setFoodPreference from the public interface and instead add methods Carnivore::setMeatPreference(Meat *meat) and Herbivore::setFruitPreference(Fruit *fruit) directly to the subclasses. That way any code which sets the food preference must know what kind of Animal and what kind of Food it is dealing with, so you can no longer write code which tries to set an incompatible food type.

Internally, both methods may set a protected Food *_preferredFood from the common base class. Or even better, call a protected void setPreferredFood(Food *food) which is a setter for a private Food* _preferredFood. That variable should definitely not be public to ensure proper encapsulation.



来源:https://stackoverflow.com/questions/33268558/liskov-substitution-principle-with-multiple-inheritance-heirachies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!