设计模式之外观模式
来源:互联网 发布:淘宝定制t恤 编辑:程序博客网 时间:2024/05/17 06:18
原文地址: 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 //请按任意键继续. . .
- 设计模式之外观
- 设计模式之外观
- 设计模式 之 外观
- 设计模式之外观
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 设计模式之外观模式
- 圆通谎报锂电池货物 被取消空运资格
- LA 4727 jump(约瑟夫环变形)
- DWR中处理集合
- 影响Lucene对文档打分的四种方式
- Android 获取网络上的图片
- 设计模式之外观模式
- commons-httpclient-3.1.jar的一些用处
- 英文NLP工具包推荐
- C语言源码的解析
- ORACLE 10g下载|ORACLE 10g下载地址|ORACLE 10g官网下载地址
- [源码分享]等比例压缩图片
- 获取number数据的类型
- poj 2488 排序交换数
- Win2008 R2下用Hyper-V管理器无法连接Hyper-V 虚拟机的问题