Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Delegation Pattern and Protected Methods

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.

like image 403
ZZ Coder Avatar asked Apr 26 '13 11:04

ZZ Coder


People also ask

What is the delegation pattern in Java?

In this article, we will learn how to use and implement the Delegation Pattern in Java with an example. It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object.

What is delegation vs inheritance in Java?

Delegation vs Inheritance in Java. Inheritance in Java programming is the process by which one class takes the property of another other class. i.e. the new classes, known as derived or child class, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or super or parent class.

What is the use of delegate in Java?

Delegation is a way of reusing and extending the behavior of a class. It works writing a new class that incorporates the functionality of the original class by using an instance of the original class and calling its methods.

How to access a protected method of a class in Java?

The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B. import p1.*;


1 Answers

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 has package access too, do you see any specific issue with this:

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?
        }
    }

Further while using delegator pattern why wrapper class extends Base class, I don't see specific need since you already have an instance of Base. To me it seems more of an decorator.

like image 128
harsh Avatar answered Oct 17 '22 01:10

harsh