设计模式之外观模式(Facade Pattern)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w_x_myself/article/details/82143676

1、定义

外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。

2、介绍

优点: 1、减少系统相互依赖。 2、提高灵活性。 3、提高了安全性。

缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。

使用场景: 1、为复杂的模块或子系统提供外界访问的模块。 2、子系统相对独立。 3、预防低水平人员带来的风险。

注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。

3、源码

3.1、头文件

#pragma once
#include <iostream>
using namespace std;
class ILetterProcess
{
public:
	ILetterProcess(void);
	virtual ~ILetterProcess(void);
	virtual void WriteContext(string context) = 0;
	virtual void FillEnvelope(string address) = 0;
	virtual void LetterIntoEnvelope() = 0;
	virtual void SendLetter() = 0;
};

LetterprocessImpl.h

#pragma once
#include "iletterprocess.h"
class CLetterProcessImpl :
	public ILetterProcess
{
public:
	CLetterProcessImpl(void);
	~CLetterProcessImpl(void);

	void WriteContext(string context);
	void FillEnvelope(string address);
	void LetterIntoEnvelope();
	void SendLetter();
};

ModenPostOffice.h

#pragma once
#include "ILetterProcess.h"
#include "LetterProcessImpl.h"
#include "LetterPolice.h"

class CModenPostOffice
{
public:
	CModenPostOffice(void);
	~CModenPostOffice(void);
	void SendLetter(string context, string address);
private:
	ILetterProcess *m_pLetterProcess;
	CLetterPolice *m_pLetterPolice;
};

 LetterPolice.h

#pragma once
#include "ILetterProcess.h"
class CLetterPolice
{
public:
    CLetterPolice(void);
    ~CLetterPolice(void);
    void CheckLetter(ILetterProcess *pLetterProcess);
};

3.2、实现

ILetterProcess.cpp

#include "ILetterProcess.h"
ILetterProcess::ILetterProcess(void)
{
}
ILetterProcess::~ILetterProcess(void)
{
}

 LetterProcessImpl.cpp

#include "LetterProcessImpl.h"
CLetterProcessImpl::CLetterProcessImpl(void)
{
}
CLetterProcessImpl::~CLetterProcessImpl(void)
{
}
void CLetterProcessImpl::WriteContext(string context)
{
	cout << "填写信的内容... ..." << endl;
}
void CLetterProcessImpl::FillEnvelope(string address)
{
	cout << "填写收件人地址及姓名... ..." << endl;
}
void CLetterProcessImpl::LetterIntoEnvelope()
{
	cout << "把信放到信封中..." << endl;
}
void CLetterProcessImpl::SendLetter()
{
	cout << "邮递信件..." << endl;
}

ModenPostOffice.cpp

#include "ModenPostOffice.h"
CModenPostOffice::CModenPostOffice(void)
{
    this->m_pLetterProcess = new CLetterProcessImpl();
    this->m_pLetterPolice = new CLetterPolice();
}
CModenPostOffice::~CModenPostOffice(void)
{
    delete m_pLetterProcess;
    delete m_pLetterPolice;
}
void CModenPostOffice::SendLetter( string context, string address )
{
    //帮忙写信
    m_pLetterProcess->WriteContext(context);
    //写好信封
    m_pLetterProcess->FillEnvelope(address);
    //警察要检查信件了
    m_pLetterPolice->CheckLetter(m_pLetterProcess);
    //把信放到信封中
    m_pLetterProcess->LetterIntoEnvelope();
    //邮递信件
    m_pLetterProcess->SendLetter();
}

LetterPolice.cpp

#include "LetterPolice.h"
CLetterPolice::CLetterPolice(void)
{
}
CLetterPolice::~CLetterPolice(void)
{
}
void CLetterPolice::CheckLetter( ILetterProcess *pLetterProcess )
{
    cout << “检查信件,此处省略一万字。” << endl;
    return;
}

Facade.cpp

#include "ILetterProcess.h"
#include "LetterProcessImpl.h"
#include "ModenPostOffice.h"

void DoItByPostOffice()
{
	CModenPostOffice modenPostOffice;
	string context = "Hello, It's me, do you know who I am? I'm your old lover. I'd like to ... ...";
	string address = "Happy Road No. 666, Beijing City, China";
	modenPostOffice.SendLetter(context, address);
}
void DoItYourself()
{
	ILetterProcess *pLetterProcess = new CLetterProcessImpl();
	pLetterProcess->WriteContext("Hello, It's me, do you know who I am? I'm your old lover. I'd like to ... ...");
	pLetterProcess->FillEnvelope("Happy Road No. 666, Beijing City, China");
	pLetterProcess->LetterIntoEnvelope();
	pLetterProcess->SendLetter();
	delete pLetterProcess;
}
int main()
{
	//现在的调用方式。对于客户来说确实简单多了。
	//如需要增加逻辑,例如让警察来检查邮件。可以在邮局里完成这项工作。
	DoItByPostOffice();

	//原来的调用方式。
	DoItYourself();

	system("pause");

	return 0;
}

4、结果

参考文献:《菜鸟教程》? ?https://blog.csdn.net/phiall/article/details/52199659博客

猜你喜欢

转载自blog.csdn.net/w_x_myself/article/details/82143676