Java 管道运行的设计模式建议
问题陈述: 我必须像管道一样处理请求。Java 管道运行的设计模式建议,java,design-patterns,java-8,Java,Design Patterns,Java 8,问题陈述: 我必须像管道一样处理请求。 例如: 当一个请求出现时,它必须经历一系列操作,如(步骤1、步骤2、步骤3…) 所以,为了实现这一点,我使用了模板设计模式 请检查并建议我是否正确实施了此问题,或者是否有更好的解决方案。 我怀疑我的方法会引入代码气味,因为我经常更改对象的值 另外,建议我&我如何使用java8来实现这一点? 谢谢 代码: package com.example.demo.design; 导入java.util.List; 公共抽象类模板{ @自动连线 私人Step1 S
例如:
当一个请求出现时,它必须经历一系列操作,如(步骤1、步骤2、步骤3…)
所以,为了实现这一点,我使用了模板设计模式 请检查并建议我是否正确实施了此问题,或者是否有更好的解决方案。
我怀疑我的方法会引入代码气味,因为我经常更改对象的值 另外,建议我&我如何使用java8来实现这一点? 谢谢 代码:
package com.example.demo.design;
导入java.util.List;
公共抽象类模板{
@自动连线
私人Step1 Step1;
@自动连线
私人步骤2步骤2;
@自动连线
私人储蓄;
List stepOutput=null;
列表步骤输出wo=null;
List stepOutputThree=null;
公共作废步骤1(字符串操作1){
步骤输出=步骤1.方法(操作1);
}
公共作废步骤2(字符串操作2){
步骤输出wo=步骤2.方法(步骤输出,操作2);
}
抽象公共无效第3步();
公共作废保存(){
保存。保存(stepOutputThree);
}
最终公共作废运行(字符串action1、字符串action2){
步骤1(行动1);
步骤2(行动2);
步骤输出wo=步骤3();
}
}
我也有同样的问题!您可以这样做:uncheckCall
方法用于处理异常
final public void run(String action1, String action2) {
//other stuffs
Stream.of(step1.method(action1))
.map(stepOutput->uncheckCall(() ->step2.method(stepOutput,action2)))
.forEach(stepOutputThree -> uncheckCall(()->save.persist(stepOutputThree)));
//.....
}
方法:
publicstatict取消选中调用(Callable Callable){
试一试{
返回callable.call();
}捕获(运行时异常e){
//抛出BusinessException.wrap(e);
}捕获(例外e){
//抛出BusinessException.wrap(e);
}
}
我也有同样的问题!您可以这样做:uncheckCall
方法用于处理异常
final public void run(String action1, String action2) {
//other stuffs
Stream.of(step1.method(action1))
.map(stepOutput->uncheckCall(() ->step2.method(stepOutput,action2)))
.forEach(stepOutputThree -> uncheckCall(()->save.persist(stepOutputThree)));
//.....
}
方法:
publicstatict取消选中调用(Callable Callable){
试一试{
返回callable.call();
}捕获(运行时异常e){
//抛出BusinessException.wrap(e);
}捕获(例外e){
//抛出BusinessException.wrap(e);
}
}
在Java 8 streams模型中,可能如下所示:
final public void run(String action1, String action2) {
Stream.of(action1) // Stream<String>
.map(s -> step1.method(s)) // Stream<List<String>>
.map(l -> step2.method(l,action2) // Stream<List<String>>
.map(l -> step3.method(l)) // Stream<List<String>>
.forEach(l -> save.persist(l));
}
最终公共作废运行(字符串action1、字符串action2){
Stream.of(action1)//Stream
.map->step1.method()//流
.map(l->step2.method(l,action2)//流
.map(l->step3.method(l))//流
.forEach(l->save.persist(l));
}
在Java 8 streams模型中,可能如下所示:
final public void run(String action1, String action2) {
Stream.of(action1) // Stream<String>
.map(s -> step1.method(s)) // Stream<List<String>>
.map(l -> step2.method(l,action2) // Stream<List<String>>
.map(l -> step3.method(l)) // Stream<List<String>>
.forEach(l -> save.persist(l));
}
最终公共作废运行(字符串action1、字符串action2){
Stream.of(action1)//Stream
.map->step1.method()//流
.map(l->step2.method(l,action2)//流
.map(l->step3.method(l))//流
.forEach(l->save.persist(l));
}
当存在“管道”、“操作顺序”等时,首先想到的设计模式是责任链,如下所示
并为您提供以下好处:
doFilter(HttpRequest、HttpResponse、FilterChain)
来调用下一个处理程序
protectedvoid-doFilter(HttpServletRequest-req、httpservletresp、FilterChain-chain){
如果(必须通知下一个供应商){
链式过滤器(req,resp);
}
}
在使用经典责任链模式的情况下,您的处理管道可能如下所示:
final public void run(String action1, String action2) {
Stream.of(action1) // Stream<String>
.map(s -> step1.method(s)) // Stream<List<String>>
.map(l -> step2.method(l,action2) // Stream<List<String>>
.map(l -> step3.method(l)) // Stream<List<String>>
.forEach(l -> save.persist(l));
}
美国石油学会
公共类StepContext{
私有映射属性=新HashMap();
公共GetT属性(字符串名称){
(T) 获取(名称);
}
public void setAttribute(字符串名称、对象值){
attributes.put(名称、值);
}
}
公共接口步骤{
无效句柄(StepContext ctx);
}
公共抽象类AbstractStep实现了{
私人的下一步;
公共抽象步骤(){
}
公共抽象步骤(下一步){
this.next=next;
}
受保护的无效下一步(StepContext ctx){
如果(下一步!=null){
下一步。处理(ctx);
}
}
}
实施
public类Step1扩展了AbstractStep{
公共步骤1(步骤下一步){
超级(下一个);
}
公共无效句柄(StepContext ctx){
字符串action1=ctx.getAttribute(“action1”);
列表输出1=剂量测量(操作1);
setAttribute(“output1”,output1);
下一步(ctx);//调用下一步
}
}
公共类Step2扩展了AbstractStep{
公共步骤2(步骤下一步){
超级(下一个);
}
公共无效句柄(StepContext ctx){
字符串action2=ctx.getAttribute(“action2”);
List output1=ctx.getAttribute(“output1”);
列表输出2=剂量测量(输出1,动作2);
setAttribute(“output2”,output2);
下一步(ctx);//调用下一步
}
}
公共类Step3扩展了AbstractStep{
公共步骤3(下一步){
超级(下一个);
}
公共无效句柄(StepContext ctx){
字符串action2=ctx.getAttribute(“action2”);
List output2=ctx.getAttribute(“output2”);
持续(输出2);
下一步(ctx);//调用下一步
}
}
客户端代码
步骤3=新步骤3(空);
步骤2=新步骤2(步骤3);
步骤1