Here we have TV class and DVD class as example:
class TV
{
public:
TV();
virtual ~TV();
void on() const;
void off() const;
};
class DVDPlayer
{
public:
DVDPlayer();
~DVDPlayer();
void SetCD() const;
void StartPlay() const;
void Eject() const;
void PowerOff() const;
};
We create Adapter which helps DVD to "pretends" TV:
class TVStyleAdapter :
public TV
{
public:
TVStyleAdapter(DVDPlayer* AdapteeDVD);
~TVStyleAdapter();
void on() const;
void off() const;
private:
DVDPlayer* DVD;
};
// override base class funcs:
void TVStyleAdapter::on() const
{
DVD->SetCD();
DVD->StartPlay();
}
void TVStyleAdapter::off() const
{
DVD->Eject();
DVD->PowerOff();
}
After that I can add "virtual" in the TV(Base) class & override on() off() functions in Adapter class. And it will be works right.
BUT the questions is:
- Can we somehow create an Adapter without ANY changes (e.g. adding "virtual") in a base ('TV') class?
- Is it possible in this situation not violate the Open Closed principle?
- If I assume using an adapter in my program in the future, should I do the virtual methods in my classes in advance?