23种设计模式(20):外观模式(2)

来源:互联网 发布:思快奇软件下载 编辑:程序博客网 时间:2024/05/22 21:03

3. 外观模式应用实例

       下面通过一个应用实例来进一步学习和理解外观模式。

 

       1. 实例说明

       某软件公司欲开发一个可应用于多个软件的文件加密模块,该模块可以对文件中的数据进行加密并将加密之后的数据存储在一个新文件中,具体的流程包括三个部分,分别是读取源文件、加密、保存加密之后的文件,其中,读取文件和保存文件使用流来实现,加密操作通过求模运算实现。这三个操作相对独立,为了实现代码的独立重用,让设计更符合单一职责原则,这三个操作的业务代码封装在三个不同的类中。

       现使用外观模式设计该文件加密模块。

      

       2. 实例类图

       通过分析,本实例结构图如图4所示。

文件加密模块结构图

       在图4中,EncryptFacade充当外观类,FileReaderCipherMachineFileWriter充当子系统类。

 

       3. 实例代码

       (1) FileReader:文件读取类,充当子系统类。

[csharp] view plain copy
  1. //FileReader.cs  
  2. using System;  
  3. using System.Text;  
  4. using System.IO;  
  5.   
  6. namespace FacadeSample  
  7. {  
  8.     class FileReader  
  9.     {  
  10.         public string Read(string fileNameSrc)   
  11.         {  
  12.        Console.Write("读取文件,获取明文:");  
  13.             FileStream fs = null;  
  14.             StringBuilder sb = new StringBuilder();  
  15.        try  
  16.             {  
  17.                 fs = new FileStream(fileNameSrc, FileMode.Open);  
  18.                 int data;  
  19.                while((data = fs.ReadByte())!= -1)   
  20.                 {  
  21.             sb = sb.Append((char)data);  
  22.                }  
  23.                fs.Close();  
  24.                Console.WriteLine(sb.ToString());  
  25.        }  
  26.        catch(FileNotFoundException e)   
  27.             {  
  28.            Console.WriteLine("文件不存在!");  
  29.        }  
  30.        catch(IOException e)   
  31.             {  
  32.            Console.WriteLine("文件操作错误!");  
  33.        }  
  34.        return sb.ToString();  
  35.         }  
  36.     }  
  37. }  

       (2) CipherMachine:数据加密类,充当子系统类。

[csharp] view plain copy
  1. //CipherMachine.cs  
  2. using System;  
  3. using System.Text;  
  4.   
  5. namespace FacadeSample  
  6. {  
  7.     class CipherMachine  
  8.     {  
  9.        public string Encrypt(string plainText)   
  10.        {  
  11.        Console.Write("数据加密,将明文转换为密文:");  
  12.        string es = "";  
  13.             char[] chars = plainText.ToCharArray();  
  14.        foreach(char ch in chars)   
  15.             {  
  16.                 string c = (ch % 7).ToString();  
  17.            es += c;  
  18.        }  
  19.             Console.WriteLine(es);  
  20.        return es;  
  21.     }  
  22.     }  
  23. }  

       (3) FileWriter:文件保存类,充当子系统类。

[csharp] view plain copy
  1. //FileWriter.cs  
  2. using System;  
  3. using System.IO;  
  4. using System.Text;  
  5.   
  6. namespace FacadeSample  
  7. {  
  8.     class FileWriter  
  9.     {  
  10.         public void Write(string encryptStr,string fileNameDes)   
  11.         {  
  12.        Console.WriteLine("保存密文,写入文件。");  
  13.             FileStream fs = null;  
  14.        try  
  15.             {  
  16.                fs = new FileStream(fileNameDes, FileMode.Create);  
  17.                 byte[] str = Encoding.Default.GetBytes(encryptStr);  
  18.                 fs.Write(str,0,str.Length);  
  19.                 fs.Flush();  
  20.                fs.Close();  
  21.        }      
  22.        catch(FileNotFoundException e)   
  23.             {  
  24.         Console.WriteLine("文件不存在!");  
  25.        }  
  26.        catch(IOException e)   
  27.             {  
  28.                 Console.WriteLine(e.Message);  
  29.            Console.WriteLine("文件操作错误!");  
  30.        }          
  31.         }  
  32.     }  
  33. }  

       (4) EncryptFacade:加密外观类,充当外观类。

[csharp] view plain copy
  1. // EncryptFacade.cs  
  2. namespace FacadeSample  
  3. {  
  4.     class EncryptFacade  
  5.     {  
  6.         //维持对其他对象的引用  
  7.          private FileReader reader;  
  8.         private CipherMachine cipher;  
  9.         private FileWriter writer;  
  10.   
  11.         public EncryptFacade()  
  12.         {  
  13.             reader = new FileReader();  
  14.             cipher = new CipherMachine();  
  15.             writer = new FileWriter();  
  16.         }  
  17.   
  18.         //调用其他对象的业务方法  
  19.          public void FileEncrypt(string fileNameSrc, string fileNameDes)  
  20.         {  
  21.             string plainStr = reader.Read(fileNameSrc);  
  22.             string encryptStr = cipher.Encrypt(plainStr);  
  23.             writer.Write(encryptStr, fileNameDes);  
  24.         }  
  25.     }  
  26. }  

       (5) Program:客户端测试类

[csharp] view plain copy
  1. //Program.cs  
  2. using System;  
  3.   
  4. namespace FacadeSample  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             EncryptFacade ef = new EncryptFacade();  
  11.             ef.FileEncrypt("src.txt""des.txt");  
  12.             Console.Read();  
  13.         }  
  14.     }  
  15. }  


        4. 结果及分析

       编译并运行程序,输出结果如下:

读取文件,获取明文:Hello world!

数据加密,将明文转换为密文:233364062325

保存密文,写入文件。

       在本实例中,对文件src.txt中的数据进行加密,该文件内容为“Hello world!”,加密之后将密文保存到另一个文件des.txt中,程序运行后保存在文件中的密文为“233364062325”。在加密类CipherMachine中,采用求模运算对明文进行加密,将明文中的每一个字符除以一个整数(本例中为7,可以由用户来进行设置)后取余数作为密文。