I have been using delegation pattern to wrap an object created by a factory in a 3rd party library. Recently, the library added a protected method in the base class and my wrapper class doesn't work any longer. Does anyone have a good solution without resorting to reflection?
This is in 3rd party library and in their package,
public class Base {
public void foo();
protected void bar(); // Newly added
}
This is in my own package,
public class MyWrapper extends Base {
private Base delegate;
public MyWrapper(Base delegate) {
this.delegate = delegate;
}
public void foo() {
delegate.foo()
}
protected void bar() {
// Don't know what to do
}
}
EDIT: My original post wasn't clear. These 2 classes are in different packages.
To answer the question why I need delegation. This is a typical use-case of Delegation/Wrapper pattern and I can't show it here in a few lines of code. The library exposes Base class but the actual object from their factory is a derived class of Base. The actual class changes depending on configuration. So I don't know what delegate is. Therefore straight inheritance pattern doesn't work here.
protectedmethods should be accessible within the same packagefoo()andbar()methods look as ifBasewere an interface or an abstract class. Could you please specify your question? In what way would you need reflection? As it is protected, you may not need to override it at all.delegate.bar(), notthis.bar()bar? I assume you won't extendMyWrapper, so why do you needbarinMyWrapperin the first place?