I made an effort to create an adapter design pattern. A simple interface using which users can connect to both old and modern media player.
The modern media player plays mp4 format, whereas the old plays only wav format. Using the class mediaPlayerInterface users can play both media types.
If you feel this is not an adapter design pattern, please comment what is missing.
#include <iostream>
#include <string>
using namespace std;
class MediaPlayer
{
public:
virtual void playSong()=0;
};
class ModernMediaPlayer : public MediaPlayer
{
public:
void playSong( )
{
cout << "Playing from modern media player" << endl;
}
};
class oldMediaPlayer: public MediaPlayer
{
public:
void playSong( )
{
cout << "Playing from old media player" << endl;
}
};
class mediaPlayerInterface
{
private:
string fileType;
public:
mediaPlayerInterface(string fType)
{
fileType=fType;
}
MediaPlayer* getMediaPlayer( )
{
if (fileType == "mp4")
{
return new ModernMediaPlayer;
}
else if (fileType == "wav")
{
return new oldMediaPlayer;
}
}
};
int main()
{
mediaPlayerInterface *mIface = new mediaPlayerInterface("mp4");
MediaPlayer *mplayer = mIface->getMediaPlayer();
mplayer->playSong();
mIface = new mediaPlayerInterface("wav");
mplayer = mIface->getMediaPlayer();
mplayer->playSong();
}
Aucun commentaire:
Enregistrer un commentaire