设计模式之外观模式

来源:互联网 发布:火车头数据采集器简介 编辑:程序博客网 时间:2024/06/04 18:20

1. 概述

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

模式中的角色
(1)外观类(Facade):外观类知道哪些子系统类负责处理请求,将客户的请求代理给恰当的子系统对象。
(2)子系统类集合(SubSystem Classes):子系统类集合实现了子系统的功能,处理外观类对象指派的任务。

优点
(1)Facade模式降低了客户端对子系统使用的复杂性。
(2)外观模式松散了客户端与子系统的耦合关系,让子系统内部的模块能更容易扩展和维护。
(3)通过合理使用Facade,可以帮助我们更好的划分访问的层次。

缺点
(1)过多的或者是不太合理的Facade也容易让人迷惑,到底是调用Facade好呢,还是直接调用模块好。

适用场景
(1)需要将设计进行分层时考虑Facade模式。
(2)在开发阶段,子系统往往因为重构变得越来越复杂,增加外观模式可以提供一个简单的接口,减少它们之间的依赖。
(3)在维护一个遗留的大型系统时,可以这个系统已经非常难以维护和扩展,可以为新系统开发一个Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。

外观模式结构图

在下图中便是通过外观模式为系统提供接口的示意图,通过添加外观类之后为系统提供了简单的接口供调用,外观类进行了大部分复杂操作。

2. 编程例子

这个例子是模拟数据库插入和删除数据库操作步骤,实现外观模式。添加数据直接调用外观模式的添加函数,删除也是同样的道理。
#pragma once#include <stdio.h>#include <iostream>#include <string>using std::cout;using std::endl;//外观模式下管理的类,获取数据库连接字符串class GetConStr{public:    GetConStr(){}    ~GetConStr(){}public:    std::string Constr()    {        std::string m_str = "user:123,pwd:123";        cout << "GetConStr get connection string:" << m_str << endl;        return m_str;    }};//外观模式下管理的类,添加一条数据到数据库中去class AddRecordInfo{public:    AddRecordInfo(){}    ~AddRecordInfo(){}public:    void AddRecod(std::string str)    {        cout << "Add record info: " << str << endl;    }};//外观模式下管理的类,删除一条数据到数据库中去class DelRecordInfo{public:    DelRecordInfo(){}    ~DelRecordInfo(){}public:    void DelRecod(std::string str)    {        cout << "Del record info: " << str << endl;    }};//外观模式下管理的类,获取数据库连接class GetCon{public:    GetCon(){}    ~GetCon(){}public:    bool Con(std::string constr)    {        cout << "get DB connect;" << endl;        if("user:123,pwd:123" == constr)            return true;        else return false;    }};//外观模式下管理的类,关闭数据库连接class CloseCon{public:    CloseCon(){}    ~CloseCon(){}public:    void close()    {        cout << "close DB connection;" << endl;    }};class DBOperate{public:    DBOperate()    {        this->p_AddInfo = nullptr;        this->p_CloseCon = nullptr;        this->p_DelInfo = nullptr;        this->p_GetCon = nullptr;        this->p_GetConStr = nullptr;    }    ~DBOperate()    {        if(this->p_AddInfo != nullptr)        {            delete this->p_AddInfo;            this->p_AddInfo = nullptr;        }        if(this->p_CloseCon = nullptr)        {            delete this->p_CloseCon;            this->p_CloseCon = nullptr;        }        if(this->p_DelInfo = nullptr)        {            delete this->p_DelInfo;            this->p_DelInfo = nullptr;        }        if(this->p_GetCon = nullptr)        {            delete this->p_GetCon;            this->p_GetCon = nullptr;        }        if(this->p_GetConStr = nullptr)        {            delete this->p_GetConStr;            this->p_GetConStr = nullptr;        }    }private:    GetConStr* p_GetConStr;         //获取数据库连接字符串    AddRecordInfo* p_AddInfo;       //添加一条信息    DelRecordInfo* p_DelInfo;       //删除一条信息    GetCon* p_GetCon;               //获取数据库连接    CloseCon* p_CloseCon;           //关闭数据库public:    //添加数据    void AddRecord(std::string m_str);    //删除数据    void DelRecord(std::string m_str)    {        std::string constr = this->p_GetConStr->Constr();        if(!this->p_GetCon->Con(constr)) return;        this->p_DelInfo->DelRecod(m_str);        this->p_CloseCon->close();    }};inline void DBOperate::AddRecord(std::string m_str){    std::string constr = this->p_GetConStr->Constr();    if(!this->p_GetCon->Con(constr)) return;    this->p_AddInfo->AddRecod(m_str);    this->p_CloseCon->close();}

int main(void){    DBOperate* p_operate = new DBOperate();     //定义装饰类对象    p_operate->AddRecord("name:keke,age:12");   //添加    cout << endl;    p_operate->DelRecord("name:meme,age=67");   //删除    delete p_operate;    p_operate = nullptr;    return 0;}

3. 运行结果


0 0