[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 s