public class Main {
public static void main(String[] args) {
pattern.CodeShapeSample br = new pattern.CodeShapeSample();
// call business rule one
br.businessRuleOne("Jack", "is man");
// call business rule two, will get an exception
try {
br.businessRuleTwoThrowException("Tom", "is woman");
}
catch (Exception e) {}
// call business rule three which has a return.
String value = br.businessRuleThree("Mary", "is woman");
}
}
代码:Code Shape 设计模式
CodeShapeSample
package pattern;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class CodeShapeSample {
/*
* This is a consumer sample
*/
public void businessRuleOne(final String name, final String value) {
CodeShapePattern.consumerShape.accept((o) -> {
// here is business rule logical
System.out.println("here is business rule logical 1.");
}, Arrays.asList(name, value));
}
/*
* This is a consumer with exception sample
*/
public void businessRuleTwoThrowException(final String name, final String value) {
CodeShapePattern.consumerShape.accept((o) -> {
// here is business rule logical
System.out.println("here is business rule logical 2.");
throw new RuntimeException("failure!");
}, Arrays.asList(name, value));
}
/*
* This is a function sample
*/
public String businessRuleThree(final String name, final String value) {
return CodeShapePattern.getFunctionShape().apply((o) -> {
// here is business rule logical
System.out.println("here is business rule logical 3.");
return name + " " + value;
}, Arrays.asList(name, value));
}
}
CodeShapePattern
package pattern;
import java.text.MessageFormat;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
public class CodeShapePattern {
public static ConsumerTwo, List
/*
* This is a consumer sample
*/
public void businessRuleOne(final String name, final String value) {
CodeShapePattern.consumerShape.accept((o) -> {
// here is business rule logical
System.out.println("here is business rule logical 1.");
}, Arrays.asList(name, value));
}
/*
* This is a function sample
*/
public String businessRuleThree(final String name, final String value) {
return CodeShapePattern.getFunctionShape().apply((o) -> {
// here is business rule logical
System.out.println("here is business rule logical 3.");
return name + " " + value;
}, Arrays.asList(name, value));
}
代码:Code Shape 设计模式 - Function
不同于consumerShape, getFunctionShape 是一个静态泛型方法,实现了统一的功能。
这个 getFunctionShape 使用了一个嵌套的 Function。 内部的 Function 是业务规则逻辑, 在业务规则逻辑,你想怎么写,就怎么写。
顺便说一句:内部的 Function的输入参数是没用的,我们可以定义一个 FunctionZero 接口来简化代码。
public static FunctionTwo, List, R> getFunctionShape() {
FunctionTwo, List, R> function = (body, params) ->
{
R ret = null;
StackTraceElement caller = new Exception().getStackTrace()[2];
String method = caller.getClassName() + "#" + caller.getMethodName();
try {
System.out.println("");
System.out.println("========");
System.out.println(MessageFormat.format("start method ''{0}''", method));
if (params != null) {
for(Object param : params) {
System.out.println(MessageFormat.format("parameter : ''{0}''", param.toString()));
}
}
System.out.println("---- start body ----");
ret = body.apply(null);
System.out.println("---- end body ----");
}
catch (Exception e) {
System.out.println(MessageFormat.format("error method ''{0}'': {1}", method, e.getMessage()));
throw e;
}
finally {
System.out.println(MessageFormat.format("end method ''{0}'', return ''{1}''", method, ret.toString()));
}
return ret;
};
return function;
}
简化版:
public static FunctionTwo, List, R> getFunctionShape() {
FunctionTwo, List, R> function = (body, params) ->
{
R ret = null;
try {
ret = body.apply(null);
}
catch (Exception e) {
throw e;
}
finally {
}
return ret;
};
return function;
}
输出结果
========
start method 'pattern.CodeShapeSample#businessRuleOne'
parameter : 'Jack'
parameter : 'is man'
---- start body ----
here is business rule logical 1.
---- end body ----
end method 'pattern.CodeShapeSample#businessRuleOne'
========
start method 'pattern.CodeShapeSample#businessRuleTwoThrowException'
parameter : 'Tom'
parameter : 'is woman'
---- start body ----
here is business rule logical 2.
error method 'pattern.CodeShapeSample#businessRuleTwoThrowException': failure!
end method 'pattern.CodeShapeSample#businessRuleTwoThrowException'
========
start method 'pattern.CodeShapeSample#businessRuleThree'
parameter : 'Mary'
parameter : 'is woman'
---- start body ----
here is business rule logical 3.
---- end body ----
end method 'pattern.CodeShapeSample#businessRuleThree', return 'Mary is woman'