Reusing Supplier, Function and Consumer objects

83 Views Asked by At

Currently I am using the CompletableFuture port from the streamsupport library to implement a chain of asynchronous tasks to perform some IO operations.

As the Function and Supplier objects became a bit too large for my taste, I wanted to move them, resulting in code like

private Function<Boolean, Boolean> getEmptyBufferFunction() {
    return new Function<Boolean, Boolean>() {
        @Override
        public Boolean apply(Boolean didPrecedingSucceed) {
            /* body */
        }
    }
};

While this works, I was curious whether this is good practice as they are re-created each time the method is called, or if a better practice is to have the implementation as final objects and re-use them. I.e.

private final Function<Boolean, Boolean> emptyBufferFunction = new Function<Boolean, Boolean>() {
        @Override
        public Boolean apply(Boolean didPrecedingSucceed) {
            /* body */
        }
    }
};
0

There are 0 best solutions below