Core Java

Wrap around design pattern in java8

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.

Ashkrit Sharma

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

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

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
James
James
8 years ago
Ashkrit
8 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 .

James
James
8 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.

Ashkrit
8 years ago
Reply to  James

Agree this will come under AOP type of behaviour injection.

Back to top button