[Java] 设计模式: Code Shape - 管理你的代码结构

Code Shape 设计模式

这里介绍一个设计模式: Code Shape。

如果你没有听说的,没问题。这个名字是我刚刚起的。

作用

在应用软件开发中,我们经常会采用多层架构。在每一层中,不同的方法往往呈现相同的代码结构。

这里我们称之为:Code Shape

比如:在数据访问层,写方法都可能有下面这些代码:

  • 获取数据库连接
  • 创建一个事务
  • 写入数据
  • 提交事务
  • 如果发生异常,回滚事务

除此之外,有时,架构师希望增加一些架构功能,比如:

  • 统一处理权限认证
  • 统一处理异常
  • 记录日志
  • 对性能做profiling
  • 记录方法的参数值

这时,设计模式 Code Shape 通过使用Lambda表达式,实现了上面的需求。

提供了一种灵活的方式,管理每层方法的代码结构。

代码示例

本位提供了一个代码示例,完成下面功能:

  • 在调用一个业务逻辑之前,写一个日志。
  • 在调用一个业务逻辑之后,写一个日志。
  • 在调用一个业务逻辑时发生异常,写一个日志。
  • 记录方法的参数值。
  • 如果有,记录返回值。

预备知识

关于 Java 8 Lambda 表达式,请参考 这里

Java 提供了 java.util.function.Consumerjava.util.function.Function,方便我们去使用Lambda表达式。

Consumer 被用于没有返回值的方法,Function 被用于有返回值的方法。

不幸的是,这两个接口只支持一个输入参数。

如果需要,我们需要写一些接口,来支持多个输入参数。

这是,提供了支持两个输入参数的例子:

  • ConsumerTwo
@FunctionalInterface
public interface ConsumerTwo<T, T2> {
public void accept(T t, T2 t2);
}
  • FunctionTwo
@FunctionalInterface
public interface FunctionTwo<T, T2, R> {
public R apply(T t, T2 t2);
}

Annotation FunctionalInterface 标示这个接口是一个function interface,内部只定义了一个方法。

代码:Main类

这个Main类调用了三个例子:

第一个例子:调用了一个没有返回值的业务逻辑方法。

第二个例子:调用了一个没有返回值的业务逻辑方法,实际上,会抛出异常。

第三个例子:调用了一个有返回值的业务逻辑方法。

代码:

  • Main.java
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.<String>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<Consumer<Object>, List<Object>> consumerShape = (body, params) ->
{
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 ----");
body.accept(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}''", method));
}
}; public static <R> FunctionTwo<Function<Object, R>, List<Object>, R> getFunctionShape() {
FunctionTwo<Function<Object, R>, List<Object>, 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;
}
}

代码说明 1:使用 Consumer

好了,这里已经提供了所有的代码。现在,让我们逐一解释。

  • 代码:业务逻辑

    由于下面这个业务规则方法没有返回值,所以使用CodeShapePattern.consumerShape

    这里有两个输入参数:

    第一个是:业务规则逻辑。

    第二个是:方法的参数值,用于内部使用。
	/*
* 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));
}
  • 代码:Code Shape 设计模式 - Consumer

    我们可以看到,consumerShape 是一个静态变量,实现了统一的功能。

    这个 consumerShape 使用了一个嵌套的 Consumer

    内部的 Consumer 是业务规则逻辑, 在业务规则逻辑,你想怎么写,就怎么写。

    顺便说一句:内部的 Consumer的输入参数是没用的,我们可以定义一个 ConsumerZero 接口来简化代码。
    public static ConsumerTwo<Consumer<Object>, List<Object>> consumerShape = (body, params) ->
{
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 ----");
body.accept(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}''", method));
}
};

简化版:

    public static ConsumerTwo<Consumer<Object>, List<Object>> consumerShape = (body, params) ->
{
try {
body.accept(null);
}
catch (Exception e) {
throw e;
}
finally {
}
};

代码说明 2:使用 Function

好了,这里已经提供了所有的代码。现在,让我们逐一解释。

  • 代码:业务逻辑

    由于下面这个业务规则方法有返回值,所以使用CodeShapePattern.<R>getFunctionShape()

    getFunctionShape()是一个泛型方法,这个泛型是业务逻辑方法的返回类型。

    这里有两个输入参数:

    第一个是:业务规则逻辑,有返回值。

    第二个是:方法的参数值,用于内部使用。
	/*
* This is a function sample
*/
public String businessRuleThree(final String name, final String value) { return CodeShapePattern.<String>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 <R> FunctionTwo<Function<Object, R>, List<Object>, R> getFunctionShape() {
FunctionTwo<Function<Object, R>, List<Object>, 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 <R> FunctionTwo<Function<Object, R>, List<Object>, R> getFunctionShape() {
FunctionTwo<Function<Object, R>, List<Object>, 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'

[Java] 设计模式:代码形状 - lambda表达式的一个应用的更多相关文章

  1. Java基础教程(23)--lambda表达式

    一.初识lambda表达式 1.定义   lambda表达式是一个可传递的代码块,或者更确切地说,可以把lambda表达式理解为简洁地表示可传递的匿名方法的一种方式.它没有名称,但它有参数列表.函数主 ...

  2. Java核心技术-接口、lambda表达式与内部类

    本章将主要介绍: 接口技术:主要用来描述类具有什么功能,而并不给出每个功能的具体实现.一个类可以实现一个或多个接口. lambda表达式:这是一种表示可以在将来的某个时间点执行的代码块的简洁方法. 内 ...

  3. Upgrading to Java 8——第一章 Lambda表达式

    第一章 Lambda表达式 Lamada 表达式是Java SE 8中最重要的新特性,长期以来被认为是在Java中缺失的特性,它的出现使整个java 语言变得完整.至少到目前,在这节中你将学习到什么是 ...

  4. Java函数式编程和lambda表达式

    为什么要使用函数式编程 函数式编程更多时候是一种编程的思维方式,是种方法论.函数式与命令式编程的区别主要在于:函数式编程是告诉代码你要做什么,而命令式编程则是告诉代码要怎么做.说白了,函数式编程是基于 ...

  5. Java 8 中的 Lambda 表达式

    Lambda 表达式是 Java 8 最受欢迎的功能.人们将函数式编程的概念引入了 Java 这门完全面向对象的命令式编程语言. 关于函数式编程是如何运作的,这个话题超出了本文的范围,不过我们会提炼出 ...

  6. Java 8新增的Lambda表达式

    一. 表达式入门 Lambda表达式支持将代码块作为方法参数,lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口(这种接口被称为函数式接口)的实例,相当于一个匿名的方法. 1.1 La ...

  7. Java基础教程:Lambda表达式

    Java基础教程:Lambda表达式 本文部分内容引用自OneAPM:http://blog.oneapm.com/apm-tech/226.html 引入Lambda Java 是一流的面向对象语言 ...

  8. Java 8 (2) 使用Lambda表达式

    什么是Lambda? 可以把Lambda表达式理解为 简洁的表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. 使用Lambda可以让你更 ...

  9. java函数式编程之lambda表达式

    作为比较老牌的面向对象的编程语言java,在对函数式编程的支持上一直不温不火. 认为面向对象式编程就应该纯粹的面向对象,于是经常看到这样的写法:如果你想写一个方法,那么就必须把它放到一个类里面,然后n ...

随机推荐

  1. Python内置函数(52)——getattr

    英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name must ...

  2. express学习(二)—— Post()类型和中间件

    1.数据:GET.POST 2.中间件:使用.写.链式操作 GET-无需中间件 req.query POST-需要"body-parser" server.use(bodyPars ...

  3. [洛谷P1198/BZOJ1012][JSOI2008] 最大数 - 树状数组/线段树?

    其实已经学了树状数组和线段树,然而懒得做题,所以至今没写多少博客 Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数 ...

  4. python/Django(增、删、改、查)操作

    python/Django(增.删.改.查)操作 我们要通过pycharm中的Django模块连接MySQL数据库进行对数据的操作. 一.创建Django项目(每创建一个项目都要进行以下设置) 1.如 ...

  5. 一日一练-CSS-CSS 居中

    特别声明:此篇文章内容来源于@CHRIS COYIER 的Centering in CSS:A Complete Guide 子曰:CSS 居中是一个非常常见的问题,无论是在项目中,还是在各种面试资料 ...

  6. Spring Cloud学习笔记-001

    Spring Boot快速入门 1. Eclipse新建maven工程,骨架选择quickstart: 2. 加入springboot的父工程,和web依赖: 3. 编写一个简单的RESTful接口, ...

  7. CodeForces 916E Jamie and Tree(树链剖分+LCA)

    To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with n vertices, numb ...

  8. Javascript中获取浏览器类型和操作系统版本等客户端信息常用代码

    /** * @author hechen */ var gs = { /**获得屏幕宽度**/ ScreenWidth: function () { return window.screen.widt ...

  9. No mapping found for HTTP request with URI [/user/login.do] in DispatcherServlet with name 'dispatcher'错误

    1.警告的相关信息 七月 24, 2017 3:53:04 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: ...

  10. [LeetCode] Reshape the Matrix 重塑矩阵

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...