基本概念
具体案例
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <crtdbg.h>
#include<unordered_map>
using namespace std;
// 手机软件
class HandsetSoft
{
public:
HandsetSoft();
~HandsetSoft();
virtual void Run() = 0;
private:
};
//手机游戏
class HandsetGame :public HandsetSoft
{
public:
HandsetGame();
~HandsetGame();
void Run() {
cout << "运行手机游戏" << endl;
}
private:
};
//手机通讯录
class HandsetAddressList :public HandsetSoft
{
public:
HandsetAddressList();
~HandsetAddressList();
void Run() {
cout << "运行手机通讯录" << endl;
}
private:
};
//MP3
class HandsetMp3 :public HandsetSoft
{
public:
HandsetMp3();
~HandsetMp3();
void Run() {
cout << "运行Mp3" << endl;
}
private:
};
//手机品牌类
class HandsetBrand
{
public:
HandsetBrand();
~HandsetBrand();
void SetHandsetSoft(HandsetSoft* mysoft) {
this->mysoft = mysoft;
}
virtual void Run() = 0;
private:
protected:
HandsetSoft* mysoft;
};
//手机品牌N
class HandsetBrandN :public HandsetBrand
{
public:
HandsetBrandN();
~HandsetBrandN();
void Run() {
mysoft->Run();
}
private:
};
//手机品牌M
class HandsetBrandM :public HandsetBrand
{
public:
HandsetBrandM();
~HandsetBrandM();
void Run() {
mysoft->Run();
}
private:
};
HandsetBrandM::HandsetBrandM()
{
}
HandsetBrandM::~HandsetBrandM()
{
}
HandsetBrandN::HandsetBrandN()
{
}
HandsetBrandN::~HandsetBrandN()
{
}
//手机品牌M
HandsetBrand::HandsetBrand()
{
}
HandsetBrand::~HandsetBrand()
{
}
HandsetAddressList::HandsetAddressList()
{
}
HandsetAddressList::~HandsetAddressList()
{
}
HandsetGame::HandsetGame()
{
}
HandsetGame::~HandsetGame()
{
}
HandsetMp3::HandsetMp3()
{
}
HandsetMp3::~HandsetMp3()
{
}
HandsetSoft::HandsetSoft()
{
}
HandsetSoft::~HandsetSoft()
{
}
int main(int argc, char** argv)
{
HandsetBrand* mybrand;
mybrand = new HandsetBrandN();
mybrand->SetHandsetSoft(new HandsetGame());
mybrand->Run();
mybrand->SetHandsetSoft(new HandsetAddressList());
mybrand->Run();
mybrand = new HandsetBrandM();
mybrand->SetHandsetSoft(new HandsetGame());
mybrand->Run();
mybrand->SetHandsetSoft(new HandsetAddressList());
mybrand->Run();
//新增Mp3,只需增加一个类
mybrand->SetHandsetSoft(new HandsetMp3());
mybrand->Run();
return 0;
}