以编程方式实现以各种指定方式组合同一接口的某些实例的接口 [英] Programmatically implementing an interface that combines some instances of the same interface in various specified ways

查看:135
本文介绍了以编程方式实现以各种指定方式组合同一接口的某些实例的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现以各种指定方式组合同一接口的某些实例的接口的最佳方法是什么?我需要为多个接口执行此操作,并且我希望最小化样板并仍然实现良好的效率,因为我需要将其用于关键生产系统。

What is the best way to implement an interface that combines some instances of the same interface in various specified ways? I need to do this for multiple interfaces and I want to minimize the boilerplate and still achieve good efficiency, because I need this for a critical production system.

这是草图问题。

抽象地说,我有一个通用的组合器类,它接受实例并指定各种组合器:

Abstractly, I have a generic combiner class which takes the instances and specify the various combinators:

class Combiner<I> {
   I[] instances;

   <T> T combineSomeWay(InstanceMethod<I,T> method) {
     // ... method.call(instances[i]) ... combined in some way ...
   }

   // more combinators
}

现在,假设我要实现跟随许多其他人的接口:

Now, let's say I want to implement the following interface among many others:

Interface Foo {
  String bar(int baz);
}

我想最终得到这样的代码:

I want to end up with code like this:

class FooCombiner implements Foo {
  Combiner<Foo> combiner;

  @Override 
  public String bar(final int baz) {
    return combiner.combineSomeWay(new InstanceMethod<Foo, String> {
      @Override public call(Foo instance) { return instance.bar(baz); } 
    });
  }
}

现在,这可以很快变长,如果接口有很多方法。我知道我可以使用Java反射API中的动态代理来实现这样的接口,但通过反射进行的方法访问要慢一百倍。那么在这种情况下样板和反射的替代方法是什么?

Now, this can quickly get long and winded if the interfaces have lots of methods. I know I could use a dynamic proxy from the Java reflection API to implement such interfaces, but method access via reflection is hundred times slower. So what are the alternatives to boilerplate and reflection in this case?

推荐答案

我会建议动态代理 - 真的是这么多吗?这些天比常规方法调用要慢 - 我听说反射对于加速重复的方法调用有很大的魔力。 (如果它慢了100倍,你确定你会注意到吗?好的,重新阅读你的问题 - 你会注意到!)

I would have suggested dynamic proxies - is it really so much slower than a regular method call these days - I've heard that reflection does quite a bit of magic under the covers to speed repeated method calls. (And if it is 100x slower, are you sure you will you notice? Ok, just re-read your question - you'll notice!)

否则,你基本上在你的问题中有解决方案:使用Command对象来包装界面中的每个方法。然后,您可以将接口集合中的每个实例传递给命令对象进行处理。

Otherwise, you basically have the solution in your question: Use a Command object to wrap each method in your interface. You can then pass each instance in the collection of interfaces to the command object for processing.

当然,如果您有勇气和冒险精神,可以生成实现您的命令对象,以及使用动态类生成的组合器接口的实现,使用cglib,javassist,orther 动态字节码生成器。这样可以避免样板。

Of course, if you're feeling brave and adventurous, you could generate the implementation of your command objects, and the implementation of the combiner interface using dynamic class generation, with cglib, javassist, orther dynamic bytecode generator. That would avoid the boilerplate.

您可能也会在方面取得一些成功,特别是带有编译时或加载时编织的aspectJ,这样可以避免反射开销。对不起,我无法提供详细信息。

You may also have some success with aspects, particularly aspectJ with compile-time or load-time weaving, so you avoid reflection overhead. Sorry I can't give details.

这篇关于以编程方式实现以各种指定方式组合同一接口的某些实例的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆