设计模式之八:外观模式(Facade)

时间:2022-03-05 08:35:57

外观模式:

为子系统中的一系列接口提供了一个统一的界面。外观模式定义了一个高层次的接口以使子系统更加easy使用。

Provide a unified interface to a set of interfaces in a subsystem.
Façade defines a higher-level interface that makes the subsystem easier to use.

UML图:

设计模式之八:外观模式(Facade)

主要包含:

  1. Facade(MortgageApplication):了解每一个子系统负者的功能;将client的请求分发给合适的子系统。
  2. SubSystem classes(Bank,Credit,Loan):实现子系统的功能。处理Facade指定的业务,这个类中没有Facede类的信息。

C++代码实现:

#include <iostream>

class SubSystem1
{
public:
void method1()
{
std::cout<<"SubSystem1::method1 invoke"<<std::endl;
}
}; class SubSystem2
{
public:
void method2()
{
std::cout<<"SubSystem2::method2 invoke"<<std::endl;
}
}; class SubSystem3
{
public:
void method3()
{
std::cout<<"SubSystem3::method3 invoke"<<std::endl;
}
}; class Facade
{
public:
Facade()
{
subSystem1=new SubSystem1();
subSystem2=new SubSystem2();
subSystem3=new SubSystem3();
}
void methodA()
{
std::cout<<"Facade methodA include:"<<std::endl;
subSystem1->method1();
subSystem2->method2();
}
void methodB()
{
std::cout<<"Facade methodB include:"<<std::endl;
subSystem1->method1();
subSystem3->method3();
}
~Facade()
{
if(subSystem1!=NULL)
delete subSystem1;
if(subSystem2!=NULL)
delete subSystem2;
if(subSystem3!=NULL)
delete subSystem3;
}
private:
SubSystem1 * subSystem1;
SubSystem2 * subSystem2;
SubSystem3 * subSystem3;
}; int main()
{
std::cout<<"外观模式測试"<<std::endl;
Facade * facade=new Facade;
facade->methodA();
facade->methodB();
delete facade;
return 0;
}

运行结果:

设计模式之八:外观模式(Facade)

一个详细的样例:

Facade为Morgage,这个类用来推断是否能对客户抵押贷款(依据客户的银行账户,行用卡记录,贷款记录来推断)。

SubSystem包含Bank(即客户的银行账户是否满足要求),Credit(客户的信用卡是否符合要求),Loan(客户的是否存在不良贷款记录)。

C++代码例如以下:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
class Customer
{
public:
Customer(string str=""):name(str)
{ }
string getName() const
{
return name;
}
void setName(string str)
{
name=str;
} private:
string name; }; class Bank
{
public:
bool hasSufficientSavings(Customer c,int amount)
{
std::cout<<"check bank for "<<c.getName()<<std::endl;
return true;
}
}; class Credit
{
public:
bool hasGoodCredit(Customer c)
{
std::cout<<"check credit for "<<c.getName()<<std::endl;
return true;
}
}; class Loan
{
public:
bool hasNoBadLoans(Customer c)
{
std::cout<<"check loans for "<<c.getName()<<std::endl;
return true;
}
}; class Mortgage
{
public:
Mortgage()
{
bank=new Bank;
credit=new Credit;
loan=new Loan;
}
bool isEligible(Customer c, int amount)
{
std::cout<<c.getName()<<" applies for "<<amount<<std::endl;
bool eligible=true;
if(!bank->hasSufficientSavings(c,amount))
{
eligible=false;
}
else if(!credit->hasGoodCredit(c))
{
eligible=false;
}
else if(!loan->hasNoBadLoans(c))
{
eligible=false;
} return eligible; }
~Mortgage()
{
if(!bank)
delete bank;
if(!credit)
delete credit;
if(!loan)
delete loan;
}
private:
Bank * bank;
Credit *credit;
Loan * loan;
}; int main()
{
std::cout<<"外观模式详细样例"<<std::endl;
Mortgage *mortgage=new Mortgage();
Customer c(string("John"));
bool eligible =mortgage->isEligible(c,25000); std::cout<<c.getName()<<" has been "<<(eligible? "approve":"reject")<<std::endl;
return 0;
}

运行结果:

设计模式之八:外观模式(Facade)

外观模式是一个对迪米特法则非常好的应用。

迪米特法则:

假设两个类不必直接通信,那么这两个类就不应当发生直接的相互作用。假设当中一个类须要调用还有一个类的某一个方法的话。能够通过第三者转发这个调用。

这也是减少耦合性的思想。