前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Java] 设计模式: Code Shape - 管理你的代码结构

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

作者头像
绿巨人
发布2018-05-18 15:37:57
9480
发布2018-05-18 15:37:57
举报
文章被收录于专栏:绿巨人专栏绿巨人专栏

[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' ```

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [Java] 设计模式: Code Shape - 管理你的代码结构
    • Code Shape 设计模式
      • 作用
        • 代码示例
          • 预备知识
            • 代码:Main类
              • 代码:Code Shape 设计模式
                • 代码说明 1:使用 Consumer
                  • 代码说明 2:使用 Function
                    • 输出结果
                    相关产品与服务
                    数据库
                    云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档