VOOZH about

URL: https://www.javacodegeeks.com/2015/10/wrap-around-design-pattern-in-java8.html

⇱ Wrap around design pattern in java8 - Java Code Geeks


Wrap around pattern is not listed in in GOF book but is very useful for problem like below:

  • Loop construct for e.g do while/while/for loop
  • Stopwatch around some code.
  • Wrap checked exception with run time exception
  • Initialization and cleanup for eg Threadpool creation/destruction or file open/close etc
  • Adding context info to threads for eg request context info for logging or passing security context etc

Lot of plumbing code is required in java to do such simple things. Java8 added lamdba support and that has answer for such problems.

Using Lambda behavior can be passed as argument to any function and it is very powerful thing if you want to solve above problems.

Wrap Around

Template of wrap around function is something like below

  • Pre Code
  • Actual behavior
  • Post Code

WrapAround for loop

@FunctionalInterface
 public interface CodeBlock {
 void execute();
 }

 @FunctionalInterface
 public interface Condition {
 boolean test();
 }

 public static void loop(Condition condition, CodeBlock codeBlock) {
 while (condition.test()) {
 codeBlock.execute();
 }
 }

Above code is very straight forward , it has 2 functional interface one for condition & another one for block of code to execute and those 2 behavior is passed to loop function using lambda.

This allows us to introduce new construct.

Lets look at some more example

WrapAround for time/stopwatch

@FunctionalInterface
 public interface CodeBlock {
 void execute();
 }
 
 public static void time(String name, CodeBlock codeBlock) {
 long start = System.currentTimeMillis();
 codeBlock.execute();
 long total = System.currentTimeMillis() - start;
 System.out.println(name + " took " + total + " ms");
 }

WrapAround Closable/Exception

@FunctionalInterface
 public interface AutoCodeBlock {
 void execute(AutoCloseable closeable) throws IOException;
 }

 @FunctionalInterface
 public interface ExceptionBlock {
 void execute() throws Exception;
 }
 
 public static void withAutoClose(AutoCloseable resource, AutoCodeBlock codeBlock) throws Exception {
 try (AutoCloseable c = resource) {
 codeBlock.execute(c);
 }
 }

 public static void wrapWithRuntimeException(ExceptionBlock codeBlock) {
 try {
 codeBlock.execute();
 } catch (Exception e) {
 throw new RuntimeException(e);
 }
 }

Java 8 has tons of feature that can make code concise and i used just one the the feature implement really useful thing.

  • Code used in blog is available @ github
Reference: Wrap around design pattern in java8 from our JCG partner Ashkrit Sharma at the Are you ready blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
Java 8
πŸ‘ Photo of Ashkrit Sharma
Ashkrit Sharma
October 5th, 2015Last Updated: October 4th, 2015
4 329 1 minute read

Ashkrit Sharma

Pragmatic software developer who loves practice that makes software development fun and likes to develop high performance & low latency system.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
James
10 years ago
0
Reply
Ashkrit
10 years ago
Reply to  James

You are right is close to Decorator pattern but i did not put reference to it because with lambda it gives flexibility to add run time behavior without overhead of inheritance. Dynamic proxy can be used to do some trick but lambda gives little flexibility in terms of syntax to add such thing .

0
Reply
James
10 years ago
Reply to  Ashkrit

I would contest that they’re the same in the case that you’re using it. Regardless even if it blurs the line between decorator or proxy I don’t think this is different. Effectively this is a case of AOP vs decorator, except it is lambdas.

0
Reply
Ashkrit
10 years ago
Reply to  James

Agree this will come under AOP type of behaviour injection.

0
Reply
Back to top button
Close
wpDiscuz