[Java] Design Pattern:Code Shape - manage your code shape

Code Shape Design Pattern

Here I will introduce a design pattern: Code Shape。

It's fine that you never heard that, I just created it.

Usage

In the enterprise application development, in most cases, we adopt multiple layers architectures.

Methods in each layer tend to present the same code structure. Here we name it as code shape.

For example, in the database access layer, writing methods would have following code:

  • Get the database connection.
  • Begin a transaction.
  • Write database.
  • Commit the transaction.
  • When there is an exception, rollback the transaction.

Beside, some times, architects hope to add some infrastructure functions, e.g.

  • Unified processing authority authentication.
  • Unified handling exceptions.
  • Logging.
  • Profiling for performance.
  • Logging method parameters.

So, the design pattern code shape implements the above requirements by using Java lambda expressions

Provide a flexible way to manage every method shape in a layer.

Code Demonstrate

This article presents a code demonstrate to implement following features:

  • Before calling a method, write a log
  • Log method parameters
  • After calling the method, write a log
  • If applicable, log the return value
  • When there is an exception, log the exception

Prerequisites

About Java 8 Lambda Expressions, please see here.

Java provides java.util.function.Consumer and java.util.function.Function, to benefit us a convenience way to use lambda expressions.

Consumer can be used by non-return methods, and Function can be used by methods which have return.

Unfortunately, they only support one argument.

Therefore, if need, we have to write interfaces to support multiple arguments.

Here are samples to support tow input arguments:

  • 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 indicates this is a function interface, only one method is defined inside.

Main class code

The main class calls 3 samples:

The first sample: it is a method without return.

The second sample: it is a method without return, but always throw an exception.

The third sample: it is a method with return.

Code:

  • 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 Design Pattern Code

  • 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;
}
}

Explanation 1:using Consumer

OK. All code is presented, let's explain them.

  • Business Rule Code

    As this business rule method does not return any thing, so I use CodeShapePattern.consumerShape.

    There are 2 input parameters.: business rule logical and the method's parameter values.
	/*
* 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 Design Pattern Code - Consumer

    We can see it, the consumerShape is a static variable that implements unified functions.

    This consumerShape uses a nested Consumer.

    The Consumer inside is the business rule logical., In business rule methods, you can write any code what you want.

    BTW, the input parameter is useless, we may define a ConsumerZero interface to simplify the code.
    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));
}
};

Less code version:

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

Explanation 2: Using Function

  • Business Rule Layer method

    As this business rule layer method has return, we use CodeShapePattern.<R>getFunctionShape() inside.

    getFunctionShape() is a generic method, the generic type is the return type of the business rule method.

    There are 2 parameters:

    The first: business rule logical, with return.

    The second: parameters of the business rule method, for internal use.
	/*
* 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 Design Pattern - Function

    Different as the consumerShape, getFunctionShape is a static generic method that implements unified functions.

    This getFunctionShape uses a nested Function.

    The Function inside is the business rule logical., In business rule methods, you can write any code what you want.

    BTW, the input parameter is useless, we may define a FunctionZero interface to simplify the code.
    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;
}

Less code version:

    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;
}

Output result


========
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] Design Pattern:Code Shape - manage your code shape的更多相关文章

  1. java design pattern - adapter pattern

    场景 适配器模式 总结 参考资料 场景 在编程中我们经常会遇到驴头不对马嘴的情况,比如以前是继承A的接口的对象,现在外部调用的时候需要该对象已B接口的形式来调用 ,这个时候我们可以让对象同时集成A和B ...

  2. Java Design Pattern(Factory,Singleton,Prototype,Proxy)

    一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...

  3. JAVA DESIGN PATTERN

    工厂模式(factory) 简单工厂模式的概念 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承 ...

  4. Java并发编程:Concurrent锁机制解析

    Java并发编程:Concurrent锁机制解析 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: # ...

  5. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

  6. Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern

    Monad typeclass不是一种类型,而是一种程序设计模式(design pattern),是泛函编程中最重要的编程概念,因而很多行内人把FP又称为Monadic Programming.这其中 ...

  7. design principle:java 回调与委派/委托机制

    博客 design principle:模拟 android Button 控件点击事件 主要说了一下模拟 android 的 Listener 模式,其实这就是一种委派与回调机制的体现. 委派,也可 ...

  8. Template Method Design Pattern in Java

    Template Method is a behavioral design pattern and it’s used to create a method stub and deferring s ...

  9. State Design Pattern

    注: 转载自 https://www.geeksforgeeks.org/state-design-pattern/  [以便查阅,非原创] State Design Pattern State pa ...

随机推荐

  1. T-SQL 标识符

    在T-SQL语言中,对SQLServer数据库及其数据对象(比如表.索引.视图.存储过程.触发器等)需要以名称来进行命名并加以区分,这些名称就称为标识符. 通常情况下,SQLServer数据库.数据库 ...

  2. [转] 29个你必须知道的Linux命令

    总结: 1. find  查找文件 2. grep 查找某个文件或者文件夹里面文件的内容 29个你必须知道的Linux命令 虽然Linux发行版支持各种各样的GUI(graphical user in ...

  3. 【cs231n】图像分类-Nearest Neighbor Classifier(最近邻分类器)【python3实现】

    [学习自CS231n课程] 转载请注明出处:http://www.cnblogs.com/GraceSkyer/p/8735908.html 图像分类: 一张图像的表示:长度.宽度.通道(3个颜色通道 ...

  4. 数据库学习之中的一个: 在 Oracle sql developer上执行SQL必知必会脚本

    1 首先在開始菜单中打开sql developer: 2. 创建数据库连接 点击左上角的加号 在弹出的对话框中填写username和password 測试假设成功则点击连接,记得角色要写SYSDBA ...

  5. virtualbox+vagrant学习-4-Vagrantfile-2-Configuration Version

    Configuration Version 配置版本是vagrant 1.1+能够与vagrant 1.0保持向后兼容的机制.同时引入了引人注目的新特性和配置选项. 如果你运行了vagrant ini ...

  6. HustOJ平台搭建

    HustOJ平台搭建非常简单,首先为了排除一些不必要的故障,可以使用阿里云的服务器更新系统盘让系统盘初始化保持在没有其他包依赖的环境下及其使用root用户. 1.针对Ubuntu14.04(根据官方文 ...

  7. vue.js数据绑定

      语法         插值             双大括号:{{text}} {{*text}}之渲染第一次 {{{html}}}        表达式(各种数值,变量,运算符的综合体)     ...

  8. iPhone将NSString转换编码集为gb2312或者gbk的方法

    很多时候软件读取的中文网页编码集是gb2312,所以显示出来的是乱码.这时需要将NSString文字编码转换.你可以试试以下代码 NSURL *url = [NSURL URLWithString:u ...

  9. 4D产品(DLG、DEM、DOM、DRG)介绍及区别

    4D产品(DLG.DEM.DOM.DRG)是什么? 4D产品是指DRG(数字栅格地图).DLG(数字线化图) .DEM(数字高程模型).DOM(数字正射影像图).4D 复合产品是将4D产品中的任意两种 ...

  10. 50道Java线程面试题(转载)

    1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务提速.比如,如果一个线程完成 ...