-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Q10Viking
committed
Mar 27, 2024
1 parent
d0b8d27
commit 92aaa9b
Showing
7 changed files
with
269 additions
and
78 deletions.
There are no files selected for viewing
Binary file added
BIN
+102 KB
docs/.vuepress/public/images/designpattern/image-20240327200617966.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+126 KB
docs/.vuepress/public/images/designpattern/image-20240327201201936.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.8 KB
docs/.vuepress/public/images/designpattern/image-20240327203226581.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /designpattern/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
|
||
|
||
## Facade模式定义 | ||
|
||
**为子系统中的一组接口提供一个一致的接口**,提供统一入口 | ||
|
||
### 生活中的例子 | ||
|
||
大家有没有比较过自己泡茶和去茶馆喝茶的区别?自己泡茶需要自行准备茶叶、茶具和开水,而去茶馆喝茶,最简单的方式就是跟茶馆服务员说想要一杯什么样的茶(铁观音、碧螺春或者西湖龙井)。正因为茶馆有服务员,顾客无须直接和茶叶、茶具、开水等交互,整个泡茶过程由服务员来完成,顾客只需与服务员交互即可,非常简单省事。 | ||
|
||
在软件开发中,有时候为了完成一项较为复杂的功能,一个类需要和多个其他业务类交互,而这些需要交互的业务类经常会作为一个完整的整体出现,由于涉及的类比较多,导致使用时代码较为复杂。此时,特别需要一个类似服务员一样的角色,由它来负责和多个业务类进行交互,而使用这些业务类的类只需和该类交互即可。外观模式通过引入一个新的外观类来实现该功能。外观类充当了软件系统中的“服务员”,它为多个业务类的调用提供了一个统一的入口,简化了类与类之间的交互。 | ||
|
||
> 在客户端代码和业务类之间增加一个外观类,由外观类来封装与业务类之间的交互,而客户端只需与外观类交互即可 | ||
Facade 模式定义了一个**高层接口**,这个接口**使得这一子系统更加容易使用** | ||
|
||
<img src="D:\Github\saas-yong\fullstack\Java架构师之路\设计模式\img\image-20210328153816988.png" alt="image-20210328153816988" style="zoom:67%;" /> | ||
|
||
|
||
|
||
## 开发优点 | ||
|
||
简化客户端的调用 | ||
|
||
|
||
|
||
## 实例代码 | ||
|
||
[Source Code](https://github.com/Q10Viking/learncode/tree/main/designpattern/src/org/hzz/facade) | ||
|
||
```java | ||
public class Facade { | ||
SubSystem1 subSystem1 = new SubSystem1(); | ||
SubSystem2 subSystem2 = new SubSystem2(); | ||
SubSystem3 subSystem3 = new SubSystem3(); | ||
|
||
public void doSomething(){ | ||
subSystem1.method(); | ||
subSystem2.method(); | ||
subSystem3.method(); | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
public class Client2 { | ||
Facade facade = new Facade(); | ||
public void doSomething2(){ | ||
facade.doSomething(); | ||
} | ||
} | ||
``` | ||
|
||
|
||
|
||
> 测试 | ||
```java | ||
public class MainTest { | ||
public static void main(String[] args) { | ||
Client1 client1 = new Client1(); | ||
Client2 client2 = new Client2(); | ||
client1.doSomething1(); | ||
client2.doSomething2(); | ||
} | ||
} | ||
``` | ||
|
||
## 应用场景 | ||
|
||
1. 当您需要使**用复杂子系统的有限但直接的接口时**,请使用Facade模式。 | ||
2. 当您想要**将子系统组织成层**时,请使用Facade。 | ||
|
||
## 源码应用 | ||
|
||
tomcat中的**requestFacade**,作为门面来隔离用户Servlet业务处理与tomcat对协议的处理 | ||
|
||
|
||
|
||
## 案例 | ||
|
||
### 文件加密模块的设计 | ||
|
||
对文件中的数据进行加密并将加密之后的数据存储在一个新文件中。具体的流程包括3个部分,分别是读取源文件、加密、保存加密之后的文件。其中,读取文件和保存文件使用流来实现,加密操作通过求模运算实现。这3个操作相对独立,为了实现代码的独立重用,让设计更符合单一职责原则,这3个操作的业务代码封装在3个不同的类中。 | ||
|
||
FileReader类用于读取文件,CipherMachine类用于对数据进行加密,FileWriter用于保存文件。由于该文件加密模块的通用性,它在多款软件中都得以使用,包括财务管理软件、公文审批系统、邮件管理系统 | ||
|
||
![image-20240327200617966](/images/designpattern/image-20240327200617966.png) | ||
|
||
> 在每次使用这3个类时,客户端代码需要与它们逐个进行交互,导致客户端代码较为复杂,且在每次使用它们时很多代码都将重复出现。 | ||
```java | ||
class Client{ | ||
public static void main(String args[]){ | ||
FileReader reader = new FileReader(); // 文件读取类 | ||
CipherMachine cipher = new CipherMachine(); // 数据加密类 | ||
FileWriter writer = new FileWriter(); // 文件保存类 | ||
|
||
String plainStr = reader.read("facade/src.txt"); // 读取源文件 | ||
String encryptStr = cipher.encrypt(plainStr); // 加密 | ||
writer.write(encryptStr,"facade/des.txt"); // 将加密结果写入新文件 | ||
} | ||
} | ||
``` | ||
|
||
为了降低系统耦合度,封装与多个子系统进行交互的代码 | ||
|
||
![image-20240327201201936](/images/designpattern/image-20240327201201936.png) | ||
|
||
#### 子系统 | ||
|
||
##### FileReader | ||
|
||
```java | ||
package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
//文件读取类:子系统类 | ||
public class FileReader { | ||
public String read(String fileNameSrc) { | ||
System.out.print("读取文件,获取明文:"); | ||
StringBuffer sb = new StringBuffer(); | ||
try{ | ||
FileInputStream inFS = new FileInputStream(fileNameSrc); | ||
int data; | ||
while((data = inFS.read())!= -1) { | ||
sb = sb.append((char)data); | ||
} | ||
inFS.close(); | ||
System.out.println(sb.toString()); | ||
} | ||
catch(FileNotFoundException e) { | ||
System.out.println("文件不存在!"); | ||
} | ||
catch(IOException e) { | ||
System.out.println("文件操作错误!"); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
``` | ||
|
||
##### FileWriter | ||
|
||
```java | ||
package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
// 文件保存类,子系统类 | ||
public class FileWriter { | ||
public void write(String encryptStr,String fileNameDes) { | ||
System.out.println("保密文件,写入文件"); | ||
try{ | ||
FileOutputStream outFS = new FileOutputStream(fileNameDes); | ||
outFS.write(encryptStr.getBytes()); | ||
outFS.close(); | ||
} | ||
catch(FileNotFoundException e) { | ||
System.out.println("文件不存在"); | ||
} | ||
catch(IOException e) { | ||
System.out.println("文件操作错误"); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
##### CipherMachine | ||
|
||
```java | ||
package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统; | ||
|
||
//数据加密类:子系统类 | ||
public class CipherMachine { | ||
public String encrypt(String plainText) { | ||
System.out.print("数据加密,将明文转换为密文:"); | ||
String es = ""; | ||
for(int i = 0; i < plainText.length(); i++) { | ||
String c = String.valueOf(plainText.charAt(i) % 7); | ||
es += c; | ||
} | ||
System.out.println(es); | ||
return es; | ||
} | ||
} | ||
|
||
``` | ||
|
||
|
||
|
||
#### 门面类 | ||
|
||
![image-20240327203159014](/images/designpattern/image-20240327203159014.png) | ||
|
||
```java | ||
package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面; | ||
|
||
import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统.CipherMachine; | ||
import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统.FileReader; | ||
import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.子系统.FileWriter; | ||
|
||
//加密外观类:外观类 | ||
public class EncryptFacade { | ||
//维持对其他对象的引用 | ||
private FileReader reader; | ||
private CipherMachine cipher; | ||
private FileWriter writer; | ||
|
||
public EncryptFacade() { | ||
reader = new FileReader(); | ||
cipher = new CipherMachine(); | ||
writer = new FileWriter(); | ||
} | ||
|
||
//调用其他对象的业务方法 | ||
public void fileEncrypt(String fileNameSrc, String fileNameDes) { | ||
String plainStr = reader.read(fileNameSrc); | ||
String encryptStr = cipher.encrypt(plainStr); | ||
writer.write(encryptStr,fileNameDes); | ||
} | ||
} | ||
``` | ||
|
||
|
||
|
||
#### 测试 | ||
|
||
![image-20240327203226581](/images/designpattern/image-20240327203226581.png) | ||
|
||
```java | ||
package org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计; | ||
|
||
import org.hzz.结构性模式.SP1_门面模式.案例.文件加密模块的设计.门面.EncryptFacade; | ||
|
||
|
||
public class TestDemo1 { | ||
public static void main(String[] args) { | ||
EncryptFacade ef = new EncryptFacade(); | ||
ef.fileEncrypt("facade//src.txt","facade//des.txt"); | ||
|
||
// File file = new File("facade//src.txt"); | ||
// System.out.println(file.getAbsolutePath()); | ||
// D:\gitee\design-patterns\springboot-design-patterns\facade\src.txt | ||
|
||
} | ||
} | ||
/** | ||
* 读取文件,获取明文:hello world | ||
* 数据加密,将明文转换为密文:63336406232 | ||
* 保密文件,写入文件 | ||
*/ | ||
``` | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters