Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How do I apply this pattern for something that returns a Future?</p> </blockquote> <p>I think you have to implement your own proxy <code>Future</code> class here. Maybe something like the following that delegates to the proxy:</p> <pre><code>public class FutureProxy implements Future&lt;Integer&gt; { private Calculator calculator; private Future&lt;Integer&gt; delegate; public FutureProxy(Calculator calculator, Future&lt;Integer&gt; delegate) { this.calculator = calculator; this.delegate = delegate; } // proxy all of the methods public boolean isDone() { return delegate.isDone(); } ... // for get, you can then call back to `calculator` public Integer get() throws InterruptedException, ExecutionException { Integer result = delegate.get(); calculator.setCachedCalculation(result); return result; } // need to handle get(long, TimeUnit) as well public Integer get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; Integer result = delegate.get(timeout, unit); calculator.setCachedCalculation(result); return result; } } </code></pre> <p>Then your decorator getter would look like:</p> <pre><code>public Future&lt;Integer&gt; getCalculation() { Future&lt;Integer&gt; future = realObject.getCalculation(); return new FutureProxy(this, future); } </code></pre> <p>As you mention in the comments, if the <code>FutureProxy</code> is an inner class to the decorator, then the <code>Calculator</code> does not been to be injected. Just make sure that you use <code>volatile</code> fields if the proxy is updating fields in <code>Calculator</code> that will be accessed by other threads.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload