Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the shortest way to delegate unimplemented methods to a contained object in Java? [closed]

I am containing the "primary object" (with most of the features) within a "helper object" that will provide convenience methods. I have only an Interface available, aside from a returned object with that interface from a factory method. I am thinking a good way to "extend" this object is composition, but the problem that my superclass must implement the primary object's interface, which would be about 600 lines of stub code.

Clearly an uncomplicated but verbose solution would be to fill out all the stubs so they merely call the primary object's methods. Is there a better way than this in Java? In other languages I'm familiar with, there would be ways to do automatic delegation for methods that are not implemented in the helper object.

Example:

class Helper implements Interface {
    Primary primary;

    Helper(Primary _primary) {
        primary = _primary;
    }

    void helper() {
        doStuff();
    }

    // 500 lines of this
    void stub() {
        primary.stub();
    }
}

Note:

Original plan was to just use regular expressions to replace all the stub TODOs in Eclipse with the actual calls. Will be looking for an Eclipse tool that does this automatically though. Also, looks like extending the interface or using Proxy is better in the end, so will be pursuing that.

like image 903
Anonymous Avatar asked Dec 20 '11 19:12

Anonymous


People also ask

What are unimplemented methods in Java?

Java already has a keyword for unimplemented methods i.e. abstract. Java has the provision where any class can implement any interface, so all the methods declared in interfaces need to be public only.

What does lombok Delegate do?

Lombok delegates all public methods of the field's type (or method's return type), as well as those of its supertypes except for all methods declared in java. lang. Object . You can pass any number of classes into the @Delegate annotation's types parameter.

How can we avoid implementing all methods interface in Java?

How can we avoid implementing all methods interface in Java? Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.

Can we leave any method of the interface from implementing in the class?

If you don't implement all methods of your interface, than you destroy the entire purpose of an interface.


2 Answers

Use Lombok's @Delegate annotation.

Second-fastest is to use your IDE; many have this functionality.

Third-fastest is to write a tiny little introspecter and dump them out yourself.

like image 194
Dave Newton Avatar answered Oct 23 '22 02:10

Dave Newton


There are a few possiblites.


First: Use an abstract implementation that delegates the calls.

abstract class InterfaceDelegator implements Interface {
  protected final Interface primary;
  public InterfaceDelegator() {
    this.primary = primary;
  }
  // implements all the calls as: primary.call(...)
}

class Helper extends InterfaceDelegator {
  // override just the methods you need
}

Second: Use the proxy (probably cleaner).

See: http://docs.oracle.com/javase/1.5.0/docs/guide/reflection/proxy.html

final Interface primary = ...;
return (Interface) Proxy.newProxyInstance(Inteface.class.getClassLoader(),
   new Class[] { Interface.class },
   new InvocationHandler() {
     public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
       // if m is a method that you want to change the behaviour of, return something
       // by default, delegate to the primary
       return m.invoke(primary, args);
     }
   });

The invocation handler will handle calls through executing a method that you have coded, an all the others would be delegated to the primary implementation. Then, you can wrap a code like this in a method or another factory.


Third: Using the IDE to generate the methods (as others pointed out).

like image 36
Kru Avatar answered Oct 23 '22 02:10

Kru