Java: delegation pattern and protected methods

I use the delegation pattern to wrap an object created by a factory in a third-party library. Recently, the library added a protected method to the base class, and my wrapper class no longer works. Does anyone have a good solution without resorting to thought?

This is in a third-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 was not clear. These 2 classes are in different packages.

, . Delegation/Wrapper, . Base, factory Base. . , . .

+5
5
Access Levels
Modifier    Class   Package Subclass    World
public      Y          Y        Y         Y
protected   Y          Y        Y         N
no modifier Y          Y        N         N
private     Y          N        N         N

protected package, - :

class Base {
        public void foo(){};

        protected void bar(){}; // Newly added
    }

    class MyWrapper  {
        private Base delegate;

        public MyWrapper(Base delegate) {
            this.delegate = delegate;
        }

        public void foo() {
            delegate.foo();
        }

        protected void bar() {
            // Don't know what to do
            delegate.bar(); //since its in same package, it can be referenced, do you expect compile time error?
        }
    }

, , - Base, , Base. .

+4

( ?) .

. , (, ) , . .

, , Base, SubBase, SubBase Base, SubBase . bar() write delegate.bar(). , SubBase , SubBase not Base

, , , , , , :

//Wrapper

 private SubBase delegate;

, , SubBase. public void pubBar() SubBase, this.bar(). , SubBase (via pubBar()), ,

+1

, Base , . , bar(), , , .

: Base , ( MyWrapper.bar() Base.bar().


, bar() API:

@Override
protected void bar() {
    delegate.bar();
}

, :

@Override
protected void bar() {
    // silently ignored
}

:

@Override
protected void bar() {
    throw new UnsupportedOperationException("not implemented");
}

, , ,

public class MyWrapper {    // does not extend Base
    private Base delegate;

    public MyWrapper(Base delegate) {
        this.delegate = delegate;
    }

    public void foo() {
        delegate.foo()
    }

    // does not have to implement bar()
}
+1

Spring Wrapper " " pojo factory.

<bean id="interfaceDelegate" factory-bean="DelegatePojoFactory"
    factory-method="getMyDelegateInstance">
    <constructor-arg index="0">
        <value>OnLine</value>
    </constructor-arg>
</bean>

<bean id="wrapper" class="path.Wrapper">
      <property name="delegate" ref="interfaceDelegate"></property>
</bean>
0

, , :

public class MyWrapper {
    private Base delegate;

    public MyWrapper(Base delegate) {
        this.delegate = delegate;
    }

    public void foo() {
        delegate.foo()
    }

}

, , , .

If the library defines an interface that provides a public method that is implemented by the abstract base class, you can intelligently implement this interface without inheriting from the abstract base class, which can also satisfy your needs.

0
source

All Articles