开发者

java create a wrapper interface for another interface

The problem I am facing is as below -

  1. I am using a 3rd party library, say Editor, which has an interface , EditorActions, with methods -

    create(), edit(), delete().

  2. I do not want to expose, EditorActions 's methods in my implementation. So my interface will have methods like -

    myCreate(), myEdit(), myDelete() which in turn should call the EditorActions methods.

    Ed开发者_如何学运维itorActions is only an interface, the implementation is internal to the library.

    How do I link the 2 interfaces without implementing either of them?

thanks for all your help


You can do this by exposing the methods that you want people to use in an abstract class. And then force people to implement the specific methods that you want them to.

You can then use the methods from the EditorActions interface as well as the methods that you force you implementations to implement.

public abstract class AbstractEditorActions {

   private EditorActions ea;

   public AbstractEditorActions(EditorActions ea) {
      this.ea = ea;
   }

   // In this method, you can use the methods
   // from the interface and from this abstract class.
   // Make the method final so people don't break
   // the implementation.
   public final void yourExposedMethod() {
      // code
      this.toImplement();
      ea.doMethod();
   }

   protected abstract toImplement();

}


Assuming you obtain an instance of EditorActions from the library you could do this:

public class FooActions implements MyEditorActions, EditorActions{
    private EditorActions internal;

    public FooActions(EditorActions internal){
        this.internal = internal;
    }

    @Override
    public void create(){
        internal.create();
    }

    @Override
    public void myCreate(){
        // do stuff
        this.create();
    }
}

What this does is wrap the instance of the library object with an object that implements the same interface as well as yours. Then, you just expose the object as whatever interface you want it to be.

EditorActions a1 = new FooActions(); // a1 only shows library methods
MyEditorActions a2 = a1; // now a2 only shows your methods


How do I link the 2 interfaces without implementing either of them?

You can't. You are trying to do automatic magic here. Don't do magic. You have to implement either one of them no matter what.

Or, you'll have to implement your own reflection plumbing (or AOP somehow) to create classes on the fly. The later is no trivial manner, and typically an overkill and a red-flag of over-engineering just to avoid implementing what amounts to be a plain-old delegate.

OTH, if you only wanted to "expose" a subset of the methods provided by a third party interface A (say, for example, only the getter methods), you could almost trivially create (by good old elbow grease or a reflection library) an interface B that only exposes that subset of methods you desire.

interface DirtyThirdPartyInterface { StupidCrap getSomeStupidCrap(); void setStupidCrap(); }

interface MySanitizedInterface { StupidCrap getSomeStupidCrap(); // the setter is not part of this interface }

Then with, say, Spring AOP or something similar or one of the several reflection libraries out there, then you could auto-generate an implementation of MySanitizedInterface as an AOP interceptor that simply proxies the call to the getter (via reflection) to the getter in the 3rd party interface.

But again, that's a lot of crap (not to mention 3rd party library dependencies) to simply avoiding what amounts to be simple hand-coding. It is rare to find a real-world case that justifies all that plumbing malarkey. If I were to run into something like that, the first thing I would think is "red flag". YMMV of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜