Note that there are some explanatory texts on larger screens.

plurals
  1. PODecorator pattern loses virtual method lookups. Any good alternatives or workarounds?
    primarykey
    data
    text
    <h2>Background</h2> <p>I'm in the following situation:</p> <p>I have an <code>Analyzer</code> type that looks roughly as follows</p> <pre><code>interface Analyzer { int computeValue(); String performAnalysis(); } </code></pre> <p>implemented by something like</p> <pre><code>class AnalyzerImpl implements Analyzer { @Override public int computeValue() { return 5; } @Override public String performAnalysis() { return "Result: " + computeValue(); } } </code></pre> <p>(In my actual code, <code>performAnalysis</code> performs many computations using several different <code>computeValue</code> methods of varying complexities.)</p> <p>Now I need to selectively tweak the behavior of an <code>Analyzer</code> object at runtime (or create a wrapper with the tweaked behavior).</p> <h2>What I tried:</h2> <p>I added the tweaking-method:</p> <pre><code>public Analyzer tweakAnalyzer(Analyzer untweakedAnalyzer) { ... } </code></pre> <p>and attempted to solve it using the <a href="http://en.wikipedia.org/wiki/Decorator_pattern" rel="nofollow">decorator pattern</a>:</p> <pre><code>class AnalyzerDecorator implements Analyzer { Analyzer delegate; public AnalyzerDecorator(Analyzer delegate) { this.delegate = delegate; } @Override public int computeValue() { return delegate.computeValue(); } @Override public String performAnalysis() { return delegate.performAnalysis(); } } </code></pre> <p>I then implemented <code>tweakAnalyzer</code> as follows:</p> <pre><code>public Analyzer tweakAnalyzer(Analyzer untweakedAnalyzer) { return new AnalyzerDecorator(untweakedAnalyzer) { @Override public int computeValue() { return 1337; // tweaked value! } }; } </code></pre> <p>However, when doing</p> <pre><code>tweakAnalyzer(new AnalyzerImpl()).performAnalysis(); </code></pre> <p>I get the <em>untweaked value</em> <code>Result: 5</code>, and not <code>Result: 1337</code> as desired.</p> <p>(This is because the tweaked analyzer is not the same object as the untweaked analyzer, it's merely a wrapper, so the call to <code>computeValue</code> in <code>AnalyzerImpl</code> doesn't work as intended.)</p> <p><a href="http://ideone.com/ajIKg" rel="nofollow">Full ideone.com example.</a></p> <h2>TL;DR:</h2> <p>I want to tweak the behavior of an object at runtime. I used the decorator pattern, but "lost" all virtual methods.</p> <h2>Question:</h2> <p>What is the best way to solve this problem, i.e. how should I tweak the behavior of an <code>Analyzer</code> in a way such that I either don't loose the virtual method lookup or such that it doesn't matter if I do?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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