标签:
在软件开发中,有时候为了完成一项较为复杂的功能,一个类需要和多个其它类进行交互,而这些需要交互的其它类通常作为一个完整的整体出现,由于涉及的类比较多,导致使用时代码较为复杂,此时,特别需要一个类似服务员一样的角色,由它复杂和多个业务类交互,使用这些业务类的类只需要和该类交互即可。外观模式,通过引入一个新的外观类来实现该功能,外观类充当了软件系统的“服务员”,它为多个业务类的调用提供了统一的入口,简化了类与类之间的交互。
(1) 不能很好地限制客户端直接使用子系统类。
(2) 如果设计不当,增加新的子系统可能需要修改外观类的源代码,违背开闭原则。
#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; //文件读取类:子系统类 class FileReader{ public: vector<string> read(string fileNameSrc){ cout<<"读取文件,获得明文:"<<endl; vector<string> plainStr; ifstream input(fileNameSrc.c_str()); string line; while(getline(input,line)){ plainStr.push_back(line); cout<<line<<endl; } return plainStr; } }; //数据加密类 :子系统类 class CipherMachine{ public: vector<string> encrypt(vector<string> plainStr){ cout<<"数据加密,将明文转换为密文:"<<endl; vector<string> encryptStr; vector<string>::iterator iter=plainStr.begin(); while(iter!=plainStr.end()){ string temp=*iter; unsigned int i=0; while(i<temp.size()){ temp[i]=temp[i]%7+'0'; i++; } cout<<temp<<endl; encryptStr.push_back(temp); ++iter; } return encryptStr; } }; //文件保存类: 子系统类 class FileWriter{ public: void write(string fileNameDest,vector<string> encryptStr){ cout<<"保存密文,写入文件"<<endl; ofstream output(fileNameDest.c_str()); vector<string>::const_iterator iter=encryptStr.begin(); while(iter!=encryptStr.end()){ output<<*iter<<endl; ++iter; } } }; //加密外观类: 外观类 //此处设计成单例外观类 class EncryptFacade{ private: //维持对其它对象的引用 FileReader *reader; CipherMachine *cipher; FileWriter *writer; EncryptFacade(){ reader=new FileReader(); cipher=new CipherMachine(); writer=new FileWriter(); } static EncryptFacade* instance; //饿汉单例模式,保证安全的多线程访问 public: //获得单例类的唯一实例 static EncryptFacade* getInstance(){ return instance; } //统一的接口,调用其它对象的业务方法 void fileEncrypt(string fileNameSrc,string fileNameDest){ vector<string> plainStr=reader->read(fileNameSrc); vector<string> encryptStr=cipher->encrypt(plainStr); writer->write(fileNameDest,encryptStr); } }; //静态成员变量,类内定义,类外初始化 EncryptFacade* EncryptFacade::instance=new EncryptFacade(); //客户端测试代码 int main(){ EncryptFacade *encryFacade=EncryptFacade::getInstance(); string fileNameSrc("plainText.txt"); string fileNameDest("encryptText.txt"); encryFacade->fileEncrypt(fileNameSrc,fileNameDest); return 0; }
#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; //文件读取类:子系统类 class FileReader{ public: vector<string> read(string fileNameSrc){ cout<<"读取文件,获得明文:"<<endl; vector<string> plainStr; ifstream input(fileNameSrc.c_str()); string line; while(getline(input,line)){ plainStr.push_back(line); cout<<line<<endl; } return plainStr; } }; //求模运算数据加密类 :子系统类 class CipherMachine{ public: vector<string> encrypt(vector<string> plainStr){ cout<<"数据加密,将明文转换为密文:"<<endl; vector<string> encryptStr; vector<string>::iterator iter=plainStr.begin(); while(iter!=plainStr.end()){ string temp=*iter; unsigned int i=0; while(i<temp.size()){ temp[i]=temp[i]%7+'0'; i++; } cout<<temp<<endl; encryptStr.push_back(temp); ++iter; } return encryptStr; } }; //移位运算数据加密类 :子系统类 class NewChiperMachine{ public: vector<string> encrypt(vector<string> plainStr){ cout<<"数据加密,将明文转换为密文:"<<endl; vector<string> encryptStr; vector<string>::iterator iter=plainStr.begin(); while(iter!=plainStr.end()){ string temp=*iter; int key=10;//设置密钥,移位次数为10 unsigned int i=0; while(i<temp.size()){ //对小写字母移位 if(temp[i]>'a' && temp[i]<'z'){ unsigned char c=temp[i];//处理汉字的情况,汉字字节最高位为1 c+=key%26; if(c>'z') c-=26; temp[i]=c; } //对大写字母移位 if(temp[i]>'A' && temp[i]<'Z'){ unsigned char c=temp[i]; c+=key%26; if(c>'Z') c-=26; temp[i]=c; } i++; } cout<<temp<<endl; encryptStr.push_back(temp); ++iter; } return encryptStr; } }; //文件保存类: 子系统类 class FileWriter{ public: void write(string fileNameDest,vector<string> encryptStr){ cout<<"保存密文,写入文件"<<endl; ofstream output(fileNameDest.c_str()); vector<string>::const_iterator iter=encryptStr.begin(); while(iter!=encryptStr.end()){ output<<*iter<<endl; ++iter; } } }; //抽象外观类 class AbstractEncryptFacade{ public: virtual void fileEncrypt(string,string)=0; }; //加密外观类: 外观类 //此处设计成单例外观类 class EncryptFacade : public AbstractEncryptFacade{ private: //维持对其它对象的引用 FileReader *reader; CipherMachine *cipher; FileWriter *writer; EncryptFacade(){ reader=new FileReader(); cipher=new CipherMachine(); writer=new FileWriter(); } static EncryptFacade* instance; //饿汉单例模式,保证安全的多线程访问 public: //获得单例类的唯一实例 static EncryptFacade* getInstance(){ return instance; } //统一的接口,调用其它对象的业务方法 void fileEncrypt(string fileNameSrc,string fileNameDest){ vector<string> plainStr=reader->read(fileNameSrc); vector<string> encryptStr=cipher->encrypt(plainStr); writer->write(fileNameDest,encryptStr); } }; //静态成员变量,类内定义,类外初始化 EncryptFacade* EncryptFacade::instance=new EncryptFacade(); //新加密外观类: 外观类 //此处设计成单例外观类 class NewEncryptFacade : public AbstractEncryptFacade{ private: //维持对其它对象的引用 FileReader *reader; NewChiperMachine *cipher; FileWriter *writer; NewEncryptFacade(){ reader=new FileReader(); cipher=new NewChiperMachine(); writer=new FileWriter(); } static NewEncryptFacade* instance; //饿汉单例模式,保证安全的多线程访问 public: //获得单例类的唯一实例 static NewEncryptFacade* getInstance(){ return instance; } //统一的接口,调用其它对象的业务方法 void fileEncrypt(string fileNameSrc,string fileNameDest){ vector<string> plainStr=reader->read(fileNameSrc); vector<string> encryptStr=cipher->encrypt(plainStr); writer->write(fileNameDest,encryptStr); } }; //静态成员变量,类内定义,类外初始化 NewEncryptFacade* NewEncryptFacade::instance=new NewEncryptFacade(); //客户端测试代码 int main(){ //针对抽象外观类编程,使用新的文件加密外观类 AbstractEncryptFacade *newEncryFacade=NewEncryptFacade::getInstance(); string fileNameSrc("plainText.txt"); string fileNameDest("encryptText.txt"); newEncryFacade->fileEncrypt(fileNameSrc,fileNameDest); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/sxhlovehmm/article/details/46912635