简体   繁体   English

扩展接口的所有实现的功能?

[英]Extending functionality of all implementations of an Interface?

I'm looking to create a set of functions which all implementations of a certain Interface can be extended to use. 我正在寻找创建一组功能,可以扩展某个接口的所有实现。 My question is whether there's a way to do this without using a proxy or manually extending each implementation of the interface? 我的问题是,是否有一种方法可以使用代理或手动扩展接口的每个实现?

My initial idea was to see if it was possible to use generics; 我最初的想法是看是否可以使用泛型。 using a parameterized type as the super type of my implementation... 使用参数化类型作为实现的超级类型...

public class NewFunctionality<T extends OldFunctionality> extends T {
    //...
}

...but this is illegal. ...但这是非法的。 I don't exactly know why this is illegal, but it does sort of feel right that it is (probably because T could itself be an interface rather than an implementation). 我不知道到底为什么这是非法的,但它确实有点感觉不对,这是(可能是因为牛逼本身可能是一个接口,而不是实现)。

Are there any other ways to achieve what I'm trying to do? 还有其他方法可以实现我的目标吗?

EDIT One example of something I might want to do is to extend java.util.List ... Using my dodgy, illegal syntax: 编辑我可能想做的一个例子是扩展java.util.List ...使用我狡猾的,非法的语法:

public class FilterByType<T extends List> extends T {

 public void retainAll(Class<?> c) {
  //..
 }

 public void removeAll(Class<?> c) {
  //..
 } 

}

You can achieve something like this using a programming pattern known as a 'decorator' (although if the interface is large then unfortunately this is a bit verbose to implement in Java because you need to write single-line implementations of every method in the interface): 您可以使用称为“装饰器”的编程模式来实现类似的功能(尽管如果接口很大,那么不幸的是,这在Java中有点冗长,因为您需要在接口中编写每个方法的单行实现) :

public class FilterByType<T> implements List<T> {

    private List<T> _list;

    public FilterByType(List<T> list) {
        this._list = list;
    }

    public void retainAll(Class<?> c) {
        //..
    }

    public void removeAll(Class<?> c) {
        //..
    }

    // Implement List<T> interface:

    public boolean add(T element) {
        return _list.add(element);
    }

    public void add(int index, T element) {
        _list.add(index, element);
    }

    // etc...

}

Alternatively, if the methods don't need to access protected members, then static helper methods are a less clucky alternative: 另外,如果方法不需要访问受保护的成员,那么静态助手方法是一种不太麻烦的选择:

public class FilterUtils {

    public static void retainAll(List<T> list, Class<?> c) {
        //..
    }

    public static void removeAll(List<T> list, Class<?> c) {
        //..
    }

}

What prevents you from just adding new methods to the interface? 是什么阻止您仅向接口添加新方法?

If you can't just add the new functionality to old interface, you could consider making another interface and then an implementation which merely implements those two. 如果不能仅将新功能添加到旧接口,则可以考虑创建另一个接口,然后考虑仅实现这两个接口的实现。 Just to be clear, in code this is what I mean: 为了清楚起见,在代码中这就是我的意思:

// Old functionality:
public interface Traveling {
    void walk();
}
// Old implementation:
public class Person implements Traveling {
    void walk() { System.out.println("I'm walking!"); }
}
// New functionality:
public interface FastTraveling {
    void run();
    void fly();
}
// New implementation, option #1:
public class SuperHero extends Person implements FastTraveling {
    void run() { System.out.println("Zoooom!"); }
    void fly() { System.out.println("To the skies!"); }
}
// New implementation, option #2:
public class SuperHero implements Traveling, FastTraveling {
    void walk() { System.out.println("I'm walking!"); }
    void run() { System.out.println("Zoooom!"); }
    void fly() { System.out.println("To the skies!"); }
}

I think it's illegal because you can not guarantee what class T will be. 我认为这是非法的,因为您不能保证将成为T类。 Also there are technical obstacles (parent's class name must be written in bytecode, but Generics information get lost in bytecode). 还有技术上的障碍(父母的班级名称必须用字节码写,但是泛型信息会用字节码丢失)。

You can use Decorator pattern like this: 您可以使用Decorator模式,如下所示:

class ListDecorator implements List {
  private List decoratingList;
  public ListDecorator(List decoratingList){
    this.decoratingList = decoratingList;
  }

  public add(){
     decoratingList.add();
  }

  ...
}

class FilterByArrayList extends ListDecorator {
  public FilterByAbstractList () {
    super(new ArrayList());
  }
}

There is a delegation/mixin framework that allows a form of this. 有一个委托/混合框架,允许使用这种形式。 You can define a new interface, implement a default implementation of that interface, then request classes which implement that interface but subclass from elsewhere in your hierarchy. 您可以定义一个新接口,实现该接口的默认实现,然后请求实现该接口的类,但可以从层次结构中的其他位置获取子类。

It's called mixins for Java , and there's a webcast right there that demonstrates it. 它被称为Javamixins ,并且那里有一个网络广播对此进行了演示。

I'm afraid it's not clear what do you want to get. 恐怕不清楚您想要得到什么。

Basically, I don't see any benefit in using 'public class NewFunctionality<T extends OldFunctionality> extends T' in comparison with 'public class NewFunctionality extends OldFunctionality' ( 'public class FilterByType<T extends List> extends T' vs 'public class FilterByType<T> implements List<T>' ) 基本上,与“公共类NewFunctionality扩展OldFunctionality”相比,使用“公共类NewFunctionality <T扩展OldFunctionality>扩展T”没有任何好处( “公共类FilterByType <T扩展List>扩展T”“公共类FilterByType <T>实现List <T>'

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

相关问题 使用 Dagger 注入接口的所有实现 - Inject all implementations of an interface using Dagger 如何使用guice自动安装接口的所有实现? - How to auto install all implementations of an interface with guice? 获取 Android 中某个接口的所有实现列表 - Get a list of all the implementations of an Interface in Android 如何为接口的所有实现实现compareTo? - How to implement compareTo for all implementations of an interface? 如何查找 Java 接口的所有实现,而不将它们全部实例化 - How to find all implementations of a Java interface, without instantiating them all 访问者模式,并非所有访问者对接口中的所有方法都有合理的实现 - Visitor pattern, not all visitors have a reasonable implementations for all methods in the interface IntelliJ Idea结构搜索所有导入接口的实现 - IntelliJ Idea structural search for implementations of all imported interface 为什么坚持接口的所有实现都扩展了基类? - Why insist all implementations of an interface extend a base class? 一种接口方法中的新参数,但与所有实现无关 - New parameter in one interface method but not relevant for all implementations 如何使用 Micronaut 注册和运行单个接口的所有实现? - How to register and run all implementations of a single interface using Micronaut?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM