设计模式之外观模式
原文地址: http://blog.csdn.net/iuhsihsow
外观模式主要是用于对底层细节的封装,当然,要结合每个系统的上层应用。
比如很多三维软件就是对DX或者OpenGL的底层细节进行了封装,可以这么说吧
下面是类图

下面是代码
// 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式 // 定义了一个高程接口,这个接口使得这一子系统更加容易使用。 // 情景假设 // 某一软件,运行时需要连接数据库,获取数据文本,假设为txt文本 // 数据库是Oracle,从数据库中取出的是加密过的文件 // 解密子系统是decryption,解密之后是一个rar,需要解压 // 解压子系统是rar // 现在创建一个facade掩盖此过程,做到用户给一个输入,就能得到输出。 // 这个例子比较简单,因为就是封装下接口,不需要用到多态之类的 // 所以每个类就只声明一个对象即可 // 最终用户知道的细节越少越好,迪米特法则 #include "stdafx.h" #include <Windows.h> //数据库子系统 class OracleDB { public: bool GetDataFromDB(long id, byte* &pBuf) { printf("Connect to oracle now!\n"); printf("Get data from Oracle now!\n"); return true; } protected: private: }; //解密子系统 class Decryption { public: bool Decrypt(byte* &pBuf) { byte *pDecryption = NULL; pBuf = pDecryption; printf("Sucessful Decrypt!\n"); return true; } protected: private: }; //解压缩子系统 class RARSys { public: bool UnCompress(byte* &pBuf, char* pPath) { byte* pUncompress = NULL; pBuf = pUncompress; printf("Sucessful Uncompress!\n"); if (WriteData(pPath)) { return true; } } protected: private: bool WriteData(char* pPath) { printf("Txt is put in this path:\n"); printf(pPath); printf("\n"); return true; } }; //外观类 class Facade { public: bool GetTxt(long id, char *pPath) { OracleDBdb; Decryptiondp; RARSysrs; byte *pBuf; db.GetDataFromDB(id, pBuf); dp.Decrypt(pBuf); rs.UnCompress(pBuf, pPath); return true; } protected: private: }; int _tmain(int argc, _TCHAR* argv[]) { Facade fd; fd.GetTxt(100, "D:\\temp.txt"); return 0; } 输出结果: //Connect to oracle now! //Get data from Oracle now! //Sucessful Decrypt! //Sucessful Uncompress! //Txt is put in this path: //D:\temp.txt //请按任意键继续. . .- 1楼smszhuang168昨天 15:55
- 看见了更想起了自己学过的设计模式...
- Re: iuhsihsow昨天 10:06
- 回复smszhuang168n前辈好。O(∩_∩)O