Skip to content

Commit 45f4635

Browse files
committed
🐳 #345#346 业务框架 InOutManager 提供扩展点
1 parent ba88679 commit 45f4635

File tree

5 files changed

+274
-71
lines changed

5 files changed

+274
-71
lines changed

common/common-core/src/main/java/com/iohao/game/action/skeleton/core/BarSkeletonBuilder.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public final class BarSkeletonBuilder {
5252
final Runners runners = new Runners();
5353
/** handler 列表 */
5454
final List<Handler> handlerList = new LinkedList<>();
55-
/** ActionCommand 执行前与执行后的逻辑钩子类 */
56-
final List<ActionMethodInOut> inOutList = new LinkedList<>();
5755
/** action class */
5856
final List<Class<?>> actionControllerClazzList = new LinkedList<>();
5957
/** 错误码 */
@@ -78,6 +76,8 @@ public final class BarSkeletonBuilder {
7876
FlowContextFactory flowContextFactory = FlowContext::new;
7977
/** 线程执行器 */
8078
ExecutorRegion executorRegion;
79+
/** InOut 插件相管理器,ActionCommand 执行前与执行后的逻辑钩子类 */
80+
InOutManager inOutManager = InOutManager.ofPipeline();
8181

8282
BarSkeletonBuilder() {
8383
}
@@ -115,10 +115,9 @@ public BarSkeleton build() {
115115
// 线程执行器
116116
.setExecutorRegion(this.executorRegion)
117117
// runners 机制
118-
.setRunners(this.runners);
119-
120-
// inout
121-
this.extractedInOut(barSkeleton);
118+
.setRunners(this.runners)
119+
// inout
120+
.setInOutManager(this.inOutManager);
122121

123122
// 构建 actionMapping
124123
this.extractedActionCommand(barSkeleton);
@@ -171,7 +170,7 @@ public BarSkeletonBuilder addHandler(Handler handler) {
171170
*/
172171
public BarSkeletonBuilder addInOut(ActionMethodInOut inOut) {
173172
Objects.requireNonNull(inOut);
174-
this.inOutList.add(inOut);
173+
this.inOutManager.addInOut(inOut);
175174
return this;
176175
}
177176

@@ -191,11 +190,6 @@ public BarSkeletonBuilder addActionParserListener(ActionParserListener listener)
191190
return this;
192191
}
193192

194-
private void extractedInOut(BarSkeleton barSkeleton) {
195-
var inOutManager = new InOutManager(this.setting, this.inOutList);
196-
barSkeleton.setInOutManager(inOutManager);
197-
}
198-
199193
private void extractedActionCommand(BarSkeleton barSkeleton) {
200194
// action 命令对象解析器
201195
var actionCommandParser = new ActionCommandParser(this)

common/common-core/src/main/java/com/iohao/game/action/skeleton/core/BarSkeletonSetting.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ public final class BarSkeletonSetting {
6060
boolean printRunners = true;
6161

6262
/** inOut 的 in 。 true 开启 */
63+
@Deprecated
6364
boolean openIn = true;
6465
/** inOut 的 out 。 true 开启 */
66+
@Deprecated
6567
boolean openOut = true;
6668

6769
/**

common/common-core/src/main/java/com/iohao/game/action/skeleton/core/InOutManager.java

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import com.iohao.game.action.skeleton.core.flow.ActionMethodInOut;
2222
import com.iohao.game.action.skeleton.core.flow.FlowContext;
23-
import lombok.AccessLevel;
24-
import lombok.experimental.FieldDefaults;
2523

2624
import java.util.List;
2725
import java.util.Objects;
@@ -33,61 +31,34 @@
3331
* @author 渔民小镇
3432
* @date 2022-03-08
3533
*/
36-
@FieldDefaults(level = AccessLevel.PRIVATE)
37-
public final class InOutManager {
38-
/** true : 开放拦截 in */
39-
final boolean openIn;
40-
/** true : 开放拦截 out */
41-
final boolean openOut;
42-
/** inOuts */
43-
final ActionMethodInOut[] inOuts;
44-
45-
InOutManager(BarSkeletonSetting setting, List<ActionMethodInOut> inOutList) {
46-
this(setting.openIn, setting.openOut, inOutList);
47-
}
48-
49-
private InOutManager(boolean openIn, boolean openOut, List<ActionMethodInOut> inOutList) {
50-
this.inOuts = new ActionMethodInOut[inOutList.size()];
51-
inOutList.toArray(this.inOuts);
52-
53-
if (inOuts.length == 0) {
54-
openIn = false;
55-
openOut = false;
56-
}
57-
58-
this.openIn = openIn;
59-
this.openOut = openOut;
60-
}
61-
62-
public void fuckIn(FlowContext flowContext) {
63-
if (!this.openIn) {
64-
return;
65-
}
66-
67-
if (this.inOuts.length == 1) {
68-
this.inOuts[0].fuckIn(flowContext);
69-
return;
70-
}
71-
72-
for (ActionMethodInOut inOut : this.inOuts) {
73-
inOut.fuckIn(flowContext);
74-
}
75-
}
34+
public interface InOutManager {
35+
/**
36+
* 执行所有 inOut fuckIn 方法
37+
*
38+
* @param flowContext flowContext
39+
*/
40+
void fuckIn(FlowContext flowContext);
7641

77-
public void fuckOut(FlowContext flowContext) {
78-
if (!this.openOut) {
79-
return;
80-
}
42+
/**
43+
* 执行所有 inOut fuckOut 方法
44+
*
45+
* @param flowContext flowContext
46+
*/
47+
void fuckOut(FlowContext flowContext);
8148

82-
if (this.inOuts.length == 1) {
83-
this.inOuts[0].fuckOut(flowContext);
84-
return;
85-
}
49+
/**
50+
* 添加 inOut
51+
*
52+
* @param inOut inOut 插件
53+
*/
54+
void addInOut(ActionMethodInOut inOut);
8655

87-
for (ActionMethodInOut inOut : this.inOuts) {
88-
inOut.fuckOut(flowContext);
89-
}
90-
}
56+
/**
57+
* 得到插件列表
58+
*
59+
* @return ActionMethodInOut list
60+
*/
61+
List<ActionMethodInOut> listInOut();
9162

9263
/**
9364
* 通过 clazz 找到对应的 inOut 对象
@@ -98,8 +69,8 @@ public void fuckOut(FlowContext flowContext) {
9869
* @see ActionMethodInOut
9970
*/
10071
@SuppressWarnings("unchecked")
101-
public <T extends ActionMethodInOut> Optional<T> getOptional(Class<? extends T> clazz) {
102-
for (ActionMethodInOut inOut : this.inOuts) {
72+
default <T extends ActionMethodInOut> Optional<T> getOptional(Class<? extends T> clazz) {
73+
for (ActionMethodInOut inOut : this.listInOut()) {
10374
if (Objects.equals(inOut.getClass(), clazz)) {
10475
return (Optional<T>) Optional.of(inOut);
10576
}
@@ -108,7 +79,21 @@ public <T extends ActionMethodInOut> Optional<T> getOptional(Class<? extends T>
10879
return Optional.empty();
10980
}
11081

111-
List<ActionMethodInOut> listInOut() {
112-
return List.of(this.inOuts);
82+
/**
83+
* 创建 InOutManager 对象实现。inOut 的执行顺序为 in ABC,out ABC
84+
*
85+
* @return InOutManager ABC, ABC
86+
*/
87+
static InOutManager ofAbcAbc() {
88+
return new AbcAbcInOutManager();
89+
}
90+
91+
/**
92+
* 创建 InOutManager 对象实现。inOut 的执行顺序为 in ABC,out CBA。(默认策略)
93+
*
94+
* @return InOutManager ABC, CBA
95+
*/
96+
static InOutManager ofPipeline() {
97+
return new PipelineInOutManager();
11398
}
114-
}
99+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* ioGame
3+
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
4+
* # iohao.com . 渔民小镇
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package com.iohao.game.action.skeleton.core;
20+
21+
import com.iohao.game.action.skeleton.core.flow.ActionMethodInOut;
22+
import com.iohao.game.action.skeleton.core.flow.FlowContext;
23+
import lombok.AccessLevel;
24+
import lombok.Getter;
25+
import lombok.Setter;
26+
import lombok.experimental.FieldDefaults;
27+
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
@FieldDefaults(level = AccessLevel.PRIVATE)
32+
final class AbcAbcInOutManager implements InOutManager {
33+
final List<ActionMethodInOut> inOutList = new ArrayList<>();
34+
35+
public void fuckIn(FlowContext flowContext) {
36+
if (inOutList.isEmpty()) {
37+
return;
38+
}
39+
40+
if (this.inOutList.size() == 1) {
41+
this.inOutList.getFirst().fuckIn(flowContext);
42+
return;
43+
}
44+
45+
for (ActionMethodInOut inOut : this.inOutList) {
46+
inOut.fuckIn(flowContext);
47+
}
48+
}
49+
50+
public void fuckOut(FlowContext flowContext) {
51+
if (inOutList.isEmpty()) {
52+
return;
53+
}
54+
55+
if (this.inOutList.size() == 1) {
56+
this.inOutList.getFirst().fuckOut(flowContext);
57+
return;
58+
}
59+
60+
for (ActionMethodInOut inOut : this.inOutList) {
61+
inOut.fuckOut(flowContext);
62+
}
63+
}
64+
65+
@Override
66+
public void addInOut(ActionMethodInOut inOut) {
67+
this.inOutList.add(inOut);
68+
}
69+
70+
public List<ActionMethodInOut> listInOut() {
71+
return this.inOutList;
72+
}
73+
}
74+
75+
final class PipelineInOutManager implements InOutManager {
76+
final List<ActionMethodInOut> inList = new ArrayList<>();
77+
final List<ActionMethodInOut> outList = new ArrayList<>();
78+
79+
@Override
80+
public void fuckIn(FlowContext flowContext) {
81+
int size = inList.size();
82+
83+
if (size == 0) {
84+
return;
85+
}
86+
87+
if (size == 1) {
88+
this.inList.getFirst().fuckIn(flowContext);
89+
return;
90+
}
91+
92+
for (ActionMethodInOut inOut : this.inList) {
93+
inOut.fuckOut(flowContext);
94+
}
95+
}
96+
97+
@Override
98+
public void fuckOut(FlowContext flowContext) {
99+
int size = outList.size();
100+
101+
if (size == 0) {
102+
return;
103+
}
104+
105+
if (size == 1) {
106+
this.outList.getFirst().fuckIn(flowContext);
107+
return;
108+
}
109+
110+
for (ActionMethodInOut inOut : this.outList) {
111+
inOut.fuckOut(flowContext);
112+
}
113+
}
114+
115+
@Override
116+
public void addInOut(ActionMethodInOut inOut) {
117+
this.inList.addLast(inOut);
118+
this.outList.addFirst(inOut);
119+
}
120+
121+
@Override
122+
public List<ActionMethodInOut> listInOut() {
123+
return this.inList;
124+
}
125+
}

0 commit comments

Comments
 (0)