dimanche 19 novembre 2017

How to handle a change in the published abstract base class?

I have developed a "Kennel" application which takes care of various kinds of Dogs. My clients are expected to admit their Dogs to my application to avail the services.

So, I had defined a generic "Dog" interface. The clients need to implement the interface to create a concrete Dog type (Say Labrador, Poodle, etc), instantiate them and admit them to my kennel application(using say, kennel::admitDog(dog *dog).

Here is the Dog abstract base class:

class Dog {
public:
    Dog()
    {

    }

    virtual ~Dog()
    {

    }

    virtual void eatFood() = 0;
    virtual void takeBath() = 0;
    virtual void play() = 0;
    virtual void sleep() = 0;
};

I published this Interface and my clients already started using it to create their own concrete Dog types. In the next version of the application, I am planning to support robotic dogs in the kennel.

Here comes the problem. The robotic dogs needs Dog::rechargeBattery() in the above abstract base class. And, it doesnt need the existing Dog::eatFood() function. Adding the Dog::rechargeBattery() to the above abstract base class would affect all the existing clients who are already using this interface. They will be forced to implement the Dog::rechargeBattery() and recompile. This may not be desirable.

  1. What is the solution at this point?
  2. What should I have been done in the initial design to have this problem eliminated?

Aucun commentaire:

Enregistrer un commentaire