您的位置:首页 > 编程语言 > C语言/C++

设计模式观后(c++还原之二十 门面模式)

2014-09-27 21:37 405 查看
//门面模式
//和字面意思一样,把看似杂乱的东西封装一个好看的门面
//作者例子:写信——填写必要信息、封好、检查、邮递、
//抽象基类
class ILetterProcess {
public:
virtual void WriteContext(string context) = 0;
virtual void FillEnvelope(string address)= 0 ;
virtual void LetterInotoEnvelope() = 0;
virtual void SendLetter() = 0;
};
//实现写信类
class LetterProcessImpl : public ILetterProcess {
public:
void WriteContext(string context) {
cout << "写信内容:" << context << endl;
}
void FillEnvelope( string address) {
cout << "写信的收件人地址:" << address << endl;
}
void LetterInotoEnvelope() {
cout << "把信放进信封" << endl;
}
void SendLetter() {
cout <<"邮递信件" << endl;
}
};
//添加一个检查的接口
class Police {
public:
void CheckLetter(ILetterProcess* p) {
cout << "信件已经检查过了" << endl;
}
};
//门面类,(现代化邮局)
class ModenPostOffice {
private:
ILetterProcess* m_pLetter;
Police* m_pPolice;
public:
ModenPostOffice():m_pLetter(new LetterProcessImpl), m_pPolice(new Police) {}
void SendLetter(string context, string address) {
m_pLetter->WriteContext(context);
m_pLetter->FillEnvelope(address);
m_pPolice->CheckLetter(m_pLetter);
m_pLetter->LetterInotoEnvelope();
m_pLetter->SendLetter();
}
};
class Client {
public:
static void main() {
ModenPostOffice* p_ModePost = new ModenPostOffice;
p_ModePost->SendLetter("Happy Today", "give me");
}
};
//这个模式就是把复杂的模提供一个外界访问接口
int _tmain(int argc, _TCHAR* argv[])
{
Client::main();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: