diff --git a/docs/.vuepress/public/images/designpattern/image-20240327204005053.png b/docs/.vuepress/public/images/designpattern/image-20240327204005053.png new file mode 100644 index 00000000000..c6c87b790a2 Binary files /dev/null and b/docs/.vuepress/public/images/designpattern/image-20240327204005053.png differ diff --git a/docs/.vuepress/public/images/designpattern/image-20240327205421549.png b/docs/.vuepress/public/images/designpattern/image-20240327205421549.png new file mode 100644 index 00000000000..0158a6b1571 Binary files /dev/null and b/docs/.vuepress/public/images/designpattern/image-20240327205421549.png differ diff --git "a/docs/designpattern/07 \351\227\250\351\235\242(\345\244\226\350\247\202)\346\250\241\345\274\217.md" "b/docs/designpattern/07 \351\227\250\351\235\242(\345\244\226\350\247\202)\346\250\241\345\274\217.md" index 40231947d97..ee37d7048b4 100644 --- "a/docs/designpattern/07 \351\227\250\351\235\242(\345\244\226\350\247\202)\346\250\241\345\274\217.md" +++ "b/docs/designpattern/07 \351\227\250\351\235\242(\345\244\226\350\247\202)\346\250\241\345\274\217.md" @@ -266,3 +266,168 @@ public class TestDemo1 { */ ``` + + +### 文件加密模块抽象门类 + +在不修改客户端代码的前提下使用新的外观类?解决方法是:引入一个抽象外观类,客户端针对抽象外观类编程,而在运行时再确定具体外观类。使其复合开闭原则。 + +> 文件加密模块中需要更换一个加密类,不再使用原有的基于求模运算的加密类CipherMachine,而改为基于移位运算的新加密类NewCipherMachine + +![image-20240327204005053](/images/designpattern/image-20240327204005053.png) + +![image-20240327205421549](/images/designpattern/image-20240327205421549.png) + +#### 新加密算法 + +```java +package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统; + +public class NewCipherMachine { + public String encrypt(String plainText) { + System.out.print("数据加密,将明文转换为密文:"); + String es = ""; + int key = 10;//设置密钥,移位数为10 + for (int i = 0; i < plainText.length(); i++) { + char c = plainText.charAt(i); + //小写字母移位 + if (c >= 'a' && c <= 'z') { + c += key % 26; + if (c > 'z') c -= 26; + if (c < 'a') c += 26; + } + //大写字母移位 + if (c >= 'A' && c <= 'Z') { + c += key % 26; + if (c > 'Z') c -= 26; + if (c < 'A') c += 26; + } + es += c; + } + System.out.println(es); + return es; + } +} +``` + + + +#### 抽象门面 + +```java +/** + * 抽象门面 + */ +public abstract class AbstractEncryptFacade { + public abstract void fileEncrypt(String fileNameSrc, String fileNameDes); +} + +``` + +#### 新门面 + +```java +package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面; + +import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统.FileReader; +import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统.FileWriter; +import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统.NewCipherMachine; + +public class NewEncryptFacade extends AbstractEncryptFacade { + private FileReader reader; + private NewCipherMachine cipher; + private FileWriter writer; + + public NewEncryptFacade() { + reader = new FileReader(); + cipher = new NewCipherMachine(); + writer = new FileWriter(); + } + + public void fileEncrypt(String fileNameSrc, String fileNameDes) { + String plainStr = reader.read(fileNameSrc); + String encryptStr = cipher.encrypt(plainStr); + writer.write(encryptStr,fileNameDes); + } +} +``` + +#### 配置文件xml + +在xml文件中指定需要的门面 + +```xml + + + org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.NewEncryptFacade + + +``` + +#### 解析xml + +解析xml并加载类,和创建实例 + +```java +package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.utils; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; + +public class XMLUtil { + //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 + public static Object getBean() { + try { + //创建DOM文档对象 + DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = dFactory.newDocumentBuilder(); + Document doc; + doc = builder.parse(new File("src//main//resources//facade//config.xml")); + + //获取包含类名的文本节点 + NodeList nl = doc.getElementsByTagName("className"); + Node classNode=nl.item(0).getFirstChild(); + String cName=classNode.getNodeValue(); + + //通过类名生成实例对象并将其返回 + Class c=Class.forName(cName); + Object obj=c.newInstance(); + return obj; + } + catch(Exception e) { + e.printStackTrace(); + return null; + } + } +} +``` + +#### 测试 + +```java +package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计; + +import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.utils.XMLUtil; +import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.AbstractEncryptFacade; + + +public class TestDemo2 { + public static void main(String[] args) { + // 使用抽象类,运行时动态决定使用哪个具体的门面。这里由配置文件决定 + AbstractEncryptFacade ef = (AbstractEncryptFacade) XMLUtil.getBean();; + assert ef != null; + ef.fileEncrypt("facade//src.txt","facade//des.txt"); + } +} +/** + * 读取文件,获取明文:hello world + * 数据加密,将明文转换为密文:rovvy gybvn + * 保密文件,写入文件 + */ +``` +