4. 抽象外观类
- using
System; -
- namespace
FacadeSample -
{
-
class NewCipherMachine -
{ -
public string Encrypt( stringplainText) -
{ -
Console.Write("数据加密,将明文转换为密文:"); -
string es "";= -
int key //设置密钥,移位数为10= 10; -
char[] chars = plainText.ToCharArray(); -
foreach(char ch inchars) -
{ -
int temp = Convert.ToInt32(ch); -
//小写字母移位 -
if (ch 'a'>= && 'z')ch <= { -
temp += key % 26; -
if (temp > 122) temp -= 26; -
if (temp < 97) temp += 26; -
} -
//大写字母移位 -
if (ch 'A'>= && 'Z')ch <= { -
temp += key % 26; -
if (temp > 90) temp -= 26; -
if (temp < 65) temp += 26; -
} -
es += ((char)temp).ToString(); -
} -
Console.WriteLine(es); -
return es; -
} -
} -
}
图5 引入抽象外观类之后的文件加密模块结构图
- namespace
FacadeSample -
{
-
abstract class AbstractEncryptFacade -
{ -
public abstract void FileEncrypt( stringfileNameSrc, stringfileNameDes); -
} -
}
- namespace
FacadeSample -
{
-
class NewEncryptFacade : AbstractEncryptFacade -
{ -
private FileReader reader; -
private NewCipherMachine cipher; -
private FileWriter writer; -
-
public NewEncryptFacade() -
{ -
reader = new FileReader(); -
cipher = new NewCipherMachine(); -
writer = new FileWriter(); -
} -
-
public override void FileEncrypt( stringfileNameSrc, stringfileNameDes) -
{ -
string plainStr = reader.Read(fileNameSrc); -
string encryptStr = cipher.Encrypt(plainStr); -
writer.Write(encryptStr, fileNameDes); -
} -
} -
}