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博客