Background
I am in the following situation:
I have an Analyzer type that looks something like this
interface Analyzer { int computeValue(); String performAnalysis(); }
implemented by something like
class AnalyzerImpl implements Analyzer { @Override public int computeValue() { return 5; } @Override public String performAnalysis() { return "Result: " + computeValue(); } }
(In my actual code, performAnalysis does a lot of calculations using several different computeValue methods for various complexities.)
Now I need to selectively customize the behavior of the Analyzer object at runtime (or create a wrapper with modified behavior).
What I tried:
I added a setup method:
public Analyzer tweakAnalyzer(Analyzer untweakedAnalyzer) { ... }
and tried to solve it using a decorator drawing :
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(); } }
Then I implemented tweakAnalyzer as follows:
public Analyzer tweakAnalyzer(Analyzer untweakedAnalyzer) { return new AnalyzerDecorator(untweakedAnalyzer) { @Override public int computeValue() { return 1337;
However, when executed
tweakAnalyzer(new AnalyzerImpl()).performAnalysis();
I get an unexpressed Result: 5 value, not Result: 1337 as desired.
(This is due to the fact that the modified analyzer is not the same object as the fuzzy analyzer, but just a shell, so the call to computeValue in AnalyzerImpl does not work as intended.)
A complete example of ideone.com.
TL DR:
I want to customize the behavior of an object at runtime. I used the decorator pattern, but βlostβ all the virtual methods.
Question:
What is the best way to solve this problem, that is, how can I configure the behavior of the Analyzer so that I either do not lose the search in the virtual method, or it does not matter if I do?