4. 结构型模式
4.1 代理模式
即中介,控制对某对象的访问。代理服务器、缓冲服务器都是这种模式。
class AbstractCommonInterface
{
public:
virtual void run() = 0;
};
class MySystem : public AbstractCommonInterface
{
public:
virtual void run()
{
cout << "running" << endl;
}
};
class MySystemProxy : public AbstractCommonInterface
{
public:
MySystemProxy(string user, string password)
{
pSystem = new MySystem;
}
virtual void run()
{
// if ( check() )
{
pSystem->run();
}
}
private:
MySystem* pSystem;
};
4.2 外观模式
Facade模式,一组具有相似功能的类群,提供一个一致简单的界面,该界面叫做facade。各个子类不直接通信。
以一个家庭影院为例:
class Device
{
public:
virtual void On() {};
virtual void Off() {};
};
class TV : public Device {};
class Speaker : public Device {};
class Microphone: public Device {};
class Game : public Device {};
class Facade
{
public:
Facade()
{
m_pTV = new TV;
m_pSpeaker = new Speaker;
m_pMicrophone = new Microphone;
m_pGame = new Game;
}
void KTVModeOn()
{
m_pTV->On();
m_pSpeaker->On();
m_pMicrophone->On();
m_pGame->Off();
}
void KTVModeOn()
{
m_pTV->On();
m_pSpeaker->On();
m_pMicrophone->Off();
m_pGame->On();
}
~Facade()
{
if (m_pTV) { delete m_pTV; }
if (m_pSpeaker) { delete m_pSpeaker; }
if (m_pMicrophone) { delete m_pMicrophone;}
if (m_pGame) { delete m_pGame;}
}
private:
TV* m_pTV;
Speaker* m_pSpeaker;
Microphone* m_pMicrophone;
Game* m_pGame;
};
4.3 适配器模式
类似电源适配器。
比如调用foreach,回调函数有两个参数时:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void operator()(int v1, int v2)
{
cout << v1 + v2 << endl;
}
};
// 目标接口(for_each的func参数)
class Target
{
virtual void operator()(int v) = 0;
};
class Adapter : public Target
{
public:
virtual void operator()(int v)
{
myprint(v, 10);
}
private:
MyPrint myprint;
};
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for_each(v.begin(), v.end(), Adapter());
cin.get();
return 0;
}
或者
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void operator()(int v1, int v2)
{
cout << v1 + v2 << endl;
}
};
// 目标接口(for_each的func参数)
class Target
{
virtual void operator()(int v) = 0;
};
class Adapter : public Target
{
public:
Adapter(int n)
{
added = n;
}
virtual void operator()(int v)
{
myprint(v, added);
}
private:
MyPrint myprint;
int added;
};
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for_each(v.begin(), v.end(), Adapter(20));
cin.get();
return 0;
}
4.4 装饰模式
一般通过继承来拓展功能,但类会很多,此时装饰该类更合理。可联想qq秀。下面是玩家穿铠甲的例子。
class AbstractPlayer
{
public:
AbstractPlayer() : m_nHP(100) {}
virtual void ShowStatus()
{
cout << m_nHP << endl;
}
public:
int m_nHP;
};
class Player1 : public AbstractPlayer{};
class AbstractEquipment : public AbstractPlayer
{
public:
AbstractEquipment(AbstractPlayer* pPlayer) : m_pPlayer(pPlayer)
{
if (pPlayer)
{
this->m_nHP = pPlayer->m_nHP;
delete pPlayer;
}
}
private:
AbstractPlayer* m_pPlayer;
};
class Armor : public AbstractEquipment
{
public:
Armor(AbstractPlayer* pPlayer) : AbstractEquipment(pPlayer)
{
this->m_nHP += 10;
}
};
int main()
{
AbstractPlayer* pPlayer = new Player1();
pPlayer = new Armor(pPlayer);
pPlayer->ShowStatus();
pPlayer = new Armor(pPlayer);
pPlayer->ShowStatus();
cin.get();
return 0;
}