diff --git a/.idea/compiler.xml b/.idea/compiler.xml index b5b2a3900cf143cd982cd9e2f1d1eab7205bcc37..ffa6d6bba80e4b56e37aae128c27f648bb839a20 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -9,6 +9,7 @@ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 0ca6300f2b75cd9cf65dbfb85f445e252995898e..30c15027f52341b84aaf549533c90b6e6967f60a 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -3,6 +3,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 8e01406de26e7a5f5d3aa77cbc9a9021ad6b372e..91581d5bb3d0e2e03e28b06da76ebff7c8132cca 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -15,6 +15,7 @@ diff --git a/README.md b/README.md index 9eee6bc5bb8182c4c4b63a3c37036b0dabcb7ba3..1781035027f7e8f2c9a4d682b48f08d24e8180e3 100644 --- a/README.md +++ b/README.md @@ -184,8 +184,12 @@ ![img.png](img/img_strategy_pattern_struct.png) ## 6.3.3 ![img.png](img/img_strategy_pattern_merits.png) - - +# 责任链模式 +# 6.4 职责链模式 +## 6.4.1 原始定义: +> 避免将一个请求的发送者和接受者耦合在一起,让多个对象都有机会处理请求,将接受请求的对象连接成一条链,并且沿着这条链传递请求,直到有一个对象能够处理它为止 +## 结构图 +![img.png](img/img_chainOfResponsibilityStruct.png) # 特技 1. 使用 Readme.md 来支持不同的语言, Readme\_en.md: 支持英文, Readme\_zh.md: 支持中文 diff --git a/byv-chainduty-patttern-15/.gitignore b/byv-chainduty-patttern-15/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5ff6309b7199129c1afe4f4ec1906e640bec48c6 --- /dev/null +++ b/byv-chainduty-patttern-15/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/byv-chainduty-patttern-15/pom.xml b/byv-chainduty-patttern-15/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..c40845200a26153e9e4c91200d54249b4b35ddb5 --- /dev/null +++ b/byv-chainduty-patttern-15/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + org.example + byv-chainduty-patttern-15 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.projectlombok + lombok + 1.18.28 + compile + + + org.dom4j + dom4j + 2.1.3 + + + jaxen + jaxen + 1.1.6 + + + + \ No newline at end of file diff --git a/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/Client.java b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/Client.java new file mode 100644 index 0000000000000000000000000000000000000000..93b244c5f4c95b81f50d2299bf1a1063cfa4f7ea --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/Client.java @@ -0,0 +1,25 @@ +package com.boyunv.chainduty.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/19 21:17 + *@version 1.0 + */ + +public class Client { + public static void main(String[] args) { + Handler a=new HandlerA(); + Handler b=new HandlerB(); + Handler c=new HandlerC(); + + //创建处理链 + a.setSuccessor(b); + b.setSuccessor(c); + + RequestData requestData = new RequestData("请求数据: ABCD"); + //调用处理链头部的方法 + a.handle(requestData); + + } +} diff --git a/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/Handler.java b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/Handler.java new file mode 100644 index 0000000000000000000000000000000000000000..8a5d5d82ed83f2231db253bde34d2872f5f039e4 --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/Handler.java @@ -0,0 +1,20 @@ +package com.boyunv.chainduty.example01; + +import lombok.Setter; + +/* + *@description + * 抽象处理z者类 + *@author boyunv + *@create 2023/8/19 21:05 + *@version 1.0 + */ + +public abstract class Handler { + //后继处理者引用 + @Setter + protected Handler successor; + + public abstract void handle(RequestData requestData); + +} diff --git a/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerA.java b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerA.java new file mode 100644 index 0000000000000000000000000000000000000000..07f523e72d31acd4235a557e7cb3a3dc1bcba779 --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerA.java @@ -0,0 +1,22 @@ +package com.boyunv.chainduty.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/19 21:12 + *@version 1.0 + */ + +public class HandlerA extends Handler { + @Override + public void handle(RequestData requestData) { + System.out.println("HandleA 执行代码逻辑处理"+requestData.getData()); + requestData.setData(requestData.getData().replace("A","")); + //判断是否继续向后处理 + if (successor!=null){ + successor.handle(requestData); + }else { + System.out.println("执行终止"); + } + } +} diff --git a/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerB.java b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerB.java new file mode 100644 index 0000000000000000000000000000000000000000..0166ed45ceb6a87b439af04a87aeda92200b4a84 --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerB.java @@ -0,0 +1,22 @@ + package com.boyunv.chainduty.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/19 21:12 + *@version 1.0 + */ + +public class HandlerB extends Handler { + @Override + public void handle(RequestData requestData) { + System.out.println("HandleA 执行代码逻辑处理"+requestData.getData()); + requestData.setData(requestData.getData().replace("B","")); + //判断是否继续向后处理 + if (successor!=null){ + successor.handle(requestData); + }else { + System.out.println("执行终止"); + } + } +} diff --git a/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerC.java b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerC.java new file mode 100644 index 0000000000000000000000000000000000000000..b9124ec4e0e7ff436b0db1e8216333ea0ae877a3 --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/HandlerC.java @@ -0,0 +1,22 @@ + package com.boyunv.chainduty.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/19 21:12 + *@version 1.0 + */ + +public class HandlerC extends Handler { + @Override + public void handle(RequestData requestData) { + System.out.println("HandleA 执行代码逻辑处理"+requestData.getData()); + requestData.setData(requestData.getData().replace("C","")); + //判断是否继续向后处理 + if (successor!=null){ + successor.handle(requestData); + }else { + System.out.println("执行终止"); + } + } +} diff --git a/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/RequestData.java b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/RequestData.java new file mode 100644 index 0000000000000000000000000000000000000000..048b5c543d53b1a0a00e6de13df715693312b47c --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/java/com/boyunv/chainduty/example01/RequestData.java @@ -0,0 +1,21 @@ +package com.boyunv.chainduty.example01; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.ToString; + +/* + *@description + * 请求封装 + *@author boyunv + *@create 2023/8/19 21:02 + *@version 1.0 + */ +@Data +@AllArgsConstructor +@ToString +public class RequestData { + private String data; + + +} diff --git a/byv-chainduty-patttern-15/src/main/resources/config.xml b/byv-chainduty-patttern-15/src/main/resources/config.xml new file mode 100644 index 0000000000000000000000000000000000000000..7cbe016568f295adf6621d9d76f83604173b20e5 --- /dev/null +++ b/byv-chainduty-patttern-15/src/main/resources/config.xml @@ -0,0 +1,4 @@ + + + com.boyunv.strategy.example03.MT1101HandlerStrategy + \ No newline at end of file diff --git a/img/img_chainOfResponsibilityStruct.png b/img/img_chainOfResponsibilityStruct.png new file mode 100644 index 0000000000000000000000000000000000000000..59adfe40a6767432513bf2c8b8344a0c7f3f9136 Binary files /dev/null and b/img/img_chainOfResponsibilityStruct.png differ