File tree Expand file tree Collapse file tree 6 files changed +130
-0
lines changed
src/main/java/com/crossoverjie/design/pattern/chainofresponsibility Expand file tree Collapse file tree 6 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .chainofresponsibility ;
2
+
3
+ import com .crossoverjie .design .pattern .chainofresponsibility .impl .CopyrightProcess ;
4
+ import com .crossoverjie .design .pattern .chainofresponsibility .impl .SensitiveWordProcess ;
5
+ import com .crossoverjie .design .pattern .chainofresponsibility .impl .TypoProcess ;
6
+
7
+ /**
8
+ * Function:
9
+ *
10
+ * @author crossoverJie
11
+ * Date: 2018/10/21 23:07
12
+ * @since JDK 1.8
13
+ */
14
+ public class Main {
15
+ public static void main (String [] args ) {
16
+ String msg = "内容内容内容==" ;
17
+
18
+ MsgProcessChain chain = new MsgProcessChain ()
19
+ .addChain (new SensitiveWordProcess ())
20
+ .addChain (new TypoProcess ())
21
+ .addChain (new CopyrightProcess ()) ;
22
+
23
+ chain .process (msg ) ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .chainofresponsibility ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ /**
7
+ * Function:
8
+ *
9
+ * @author crossoverJie
10
+ * Date: 2018/10/22 00:08
11
+ * @since JDK 1.8
12
+ */
13
+ public class MsgProcessChain {
14
+
15
+ private List <Process > chains = new ArrayList <>() ;
16
+
17
+ /**
18
+ * 添加责任链
19
+ * @param process
20
+ * @return
21
+ */
22
+ public MsgProcessChain addChain (Process process ){
23
+ chains .add (process ) ;
24
+ return this ;
25
+ }
26
+
27
+ /**
28
+ * 执行处理
29
+ * @param msg
30
+ */
31
+ public void process (String msg ){
32
+ for (Process chain : chains ) {
33
+ chain .doProcess (msg );
34
+ }
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .chainofresponsibility ;
2
+
3
+ /**
4
+ * Function:
5
+ *
6
+ * @author crossoverJie
7
+ * Date: 2018/10/21 23:06
8
+ * @since JDK 1.8
9
+ */
10
+ public interface Process {
11
+
12
+ /**
13
+ * 执行处理
14
+ * @param msg
15
+ */
16
+ void doProcess (String msg ) ;
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .chainofresponsibility .impl ;
2
+
3
+ import com .crossoverjie .design .pattern .chainofresponsibility .Process ;
4
+
5
+ /**
6
+ * Function:
7
+ *
8
+ * @author crossoverJie
9
+ * Date: 2018/10/21 23:56
10
+ * @since JDK 1.8
11
+ */
12
+ public class CopyrightProcess implements Process {
13
+
14
+ @ Override
15
+ public void doProcess (String msg ) {
16
+ System .out .println (msg + "版权处理" );
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .chainofresponsibility .impl ;
2
+
3
+ import com .crossoverjie .design .pattern .chainofresponsibility .Process ;
4
+
5
+ /**
6
+ * Function:
7
+ *
8
+ * @author crossoverJie
9
+ * Date: 2018/10/21 23:56
10
+ * @since JDK 1.8
11
+ */
12
+ public class SensitiveWordProcess implements Process {
13
+ @ Override
14
+ public void doProcess (String msg ) {
15
+ System .out .println (msg + "敏感词处理" );
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .crossoverjie .design .pattern .chainofresponsibility .impl ;
2
+
3
+ import com .crossoverjie .design .pattern .chainofresponsibility .Process ;
4
+
5
+ /**
6
+ * Function:
7
+ *
8
+ * @author crossoverJie
9
+ * Date: 2018/10/21 23:56
10
+ * @since JDK 1.8
11
+ */
12
+ public class TypoProcess implements Process {
13
+ @ Override
14
+ public void doProcess (String msg ) {
15
+ System .out .println (msg + "错别字处理" );
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments