简体   繁体   English

java 为另一个接口创建包装接口

[英]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 -我正在使用第 3 方库,比如 Editor,它有一个接口EditorActions和方法 -

    create(), edit(), delete().创建(),编辑(),删除()。

  2. I do not want to expose, EditorActions 's methods in my implementation.我不想在我的实现中公开EditorActions的方法。 So my interface will have methods like -所以我的界面会有这样的方法 -

    myCreate(), myEdit(), myDelete() which in turn should call the EditorActions methods. myCreate()、myEdit()、myDelete()依次调用 EditorActions 方法。

    EditorActions is only an interface, the implementation is internal to the library. EditorActions 只是一个接口,实现是库内部的。

    How do I link the 2 interfaces without implementing either of them?如何在不实现其中任何一个的情况下链接 2 个接口?

thanks for all your help感谢你的帮助

You can do this by exposing the methods that you want people to use in an abstract class.您可以通过在抽象 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.然后,您可以使用EditorActions接口中的方法以及您强制实现实现的方法。

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:假设您从库中获取EditorActions的实例,您可以这样做:

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.这样做是用实现与您的接口相同的接口的 object 包装库 object 的实例。 Then, you just expose the object as whatever interface you want it to be.然后,您只需将 object 公开为您想要的任何接口。

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?如何在不实现其中任何一个的情况下链接 2 个接口?

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.或者,您必须实现自己的反射管道(或某种 AOP)来动态创建类。 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. OTH,如果您只想“公开”第三方接口A提供的方法的子集(例如,只有 getter 方法),您几乎可以轻松创建(通过好的旧肘油或反射库)只公开您想要的方法子集的接口B。

interface DirtyThirdPartyInterface { StupidCrap getSomeStupidCrap();接口 DirtyThirdPartyInterface { StupidCrap getSomeStupidCrap(); void setStupidCrap();无效 setStupidCrap(); } }

interface MySanitizedInterface { StupidCrap getSomeStupidCrap();接口 MySanitizedInterface { StupidCrap getSomeStupidCrap(); // the setter is not part of this interface } // setter 不是这个接口的一部分 }

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.然后使用 Spring AOP 或类似的东西或几个反射库之一,然后您可以自动生成 MySanitizedInterface 的实现作为 AOP 拦截器,它只是代理对 getter 的调用(通过反射)到 getter第 3 方接口。

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.但是再一次,为了简单地避免相当于简单的手工编码,这是很多废话(更不用说第 3 方库依赖项了)。 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.当然是 YMMV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM