/ Published in: Java
The idea is to create a proxy class that will delegate the increment and decrement call to MyIncDec implementation while measuring the time. To make it as transparent as possible, the proxy extends the IncDec interface. Beacuse a dependency to the actual IncDec implementation is required, the proxy constructor requires this implementation to be passed in as a parameter.
Furthermore, the code of the increment and decrement methods will still measure the duration even if an exception is thrown.
Because time measuring (performance analysis) is a cross cutting concern in a real system I would use AOP to define the timing logic just once and apply it to any method as required.
Furthermore, the code of the increment and decrement methods will still measure the duration even if an exception is thrown.
Because time measuring (performance analysis) is a cross cutting concern in a real system I would use AOP to define the timing logic just once and apply it to any method as required.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class IncDecTimerAdapter implements IncDec { private static Logger logger = ...; private final IncDec incDecToTime; public IncDecTimerAdapter(IncDec incDecToTime) { this.incDecToTime = incDecToTime; } public void increment() { try { incDecToTime.increment(); } finally { logger.debug("Time to increment: %0 (in millis)", duration); } } public void decrement() { try { incDecToTime.decrement(); } finally { logger.debug("Time to decrement: %0 (in millis)", duration); } } }