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

//门面模式
//和字面意思一样,把看似杂乱的东西封装一个好看的门面
//作者例子:写信——填写必要信息、封好、检查、邮递、
//抽象基类
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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值