结构型设计模式---外观模式(Facade)

外观模式(Facade):为子系统中的一组接口提供一个一致的界面,
Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

假设有一个抵押系统,存在三个子系统,银行子系统(查询是否有足够多的存款)、
信用子系统(查询是否有良好的信用)以及贷款子系统(查询有无贷款劣迹),
只有这三个子系统都通过时才可进行抵押。

当客户申请抵押贷款时,他需要分别去这三个子系统办理相关手续,
此时若使用外观模式为三个子系统中的接口提供一个统一的服务,
这时,客户再办理手续就只需要去一个地方了


优点:
(1) 松散耦合
    外观模式松散了客户端与子系统的耦合关系,让子系统内部的模块能更容易扩展和维护。
(2) 简单易用
    外观模式让子系统更加易用,客户端不再需要了解子系统内部的实现,
也不需要跟众多子系统内部的模块进行交互,只需要跟外观交互就可以了,
相当于外观类为外部客户端使用子系统提供了一站式服务。
(3) 更好的划分访问层次
     通过合理使用Facade,可以帮助我们更好的划分访问的层次。
有些方法是对系统外的,有些方法是系统内部使用的。
把需要暴露给外部的功能集中到外观中,这样既方便客户端使用,也很好的隐藏了内部的细节。

 

Facade.h

#ifndef _FACADE_H_
#define _FACADE_H_

#include <string>
using std::string;

class Customer
{
public:
    Customer(const string & name);
    const string & getName();

private:
    string name_;

};

class Bank
{
public:
    bool hasSufficientSavings(Customer *c, int acount);

};

class Credit
{
public:
    bool hasGoodCredit(Customer *c, int account);

};

class Loan
{
public:
    bool hasGoodCredit(Customer *c, int account);

};

// the facade class
class Mortgage
{
public:
    Mortgage();
    ~Mortgage();
    bool isEligible(Customer *c, int account);

private:
    Bank *bank_;
    Loan *loan_;
    Credit *credit_;

};

#endif
       

Facade.cpp

#include "Facade.h"
#include <iostream>
using std::cout;
using std::endl;

Customer::Customer(const string & name) : name_(name)
{
}

const string & Customer::getName()
{
     return name_;
}

bool Bank::hasSufficientSavings(Customer *c, int account)
{
     cout << "check bank for " << c->getName() << endl;
     return true;
}

bool Credit::hasGoodCredit(Customer *c, int account)
{
     cout << "check credit for " << c->getName() << endl;
     return true;
}

bool Loan::hasGoodCredit(Customer *c, int account)
{
     cout << "check loan for " << c->getName() << endl;
     return true;
}

Mortgage::Mortgage()
{
        bank_ = new Bank;
        loan_ = new Loan;
        credit_ = new Credit;
}

Mortgage::~Mortgage()
{
        if (NULL != bank_)
        {
                delete bank_;
                bank_ = NULL;
        }

        if (NULL != loan_)
        {
                delete loan_;
                loan_ = NULL;
        }

        if (NULL != credit_)
        {
                delete credit_;
                credit_ = NULL;
        }
}

bool Mortgage::isEligible(Customer *c, int account)
{
     cout << c->getName() << " apply for a loan for " << account <<  endl;
     bool eligible = true;
     eligible = bank_->hasSufficientSavings(c, account);
     if (eligible)
        eligible = loan_->hasGoodCredit(c, account);
     if (eligible)
        eligible = credit_->hasGoodCredit(c, account);
     return eligible;
}

main.cpp

#include <iostream>
#include "Facade.h"
using std::cout;
using std::endl;

int main()
{
    Mortgage mort;
    Customer c("Tom");
    bool eligible = mort.isEligible(&c, 100);
    cout << "\n" << c.getName() << " has been " ;
    cout << (eligible ? "Approved" : "Rejected") << endl;

    return 0;
}


output:

Tom apply for a loan for 100
check bank for Tom
check loan for Tom
check credit for Tom

Tom has been Approved

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值