何时在Java中使用单方法接口 [英] When to use Single method Interfaces in Java

查看:119
本文介绍了何时在Java中使用单方法接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在许多库中看到过像 Spring 这些库中使用了很多带单一方法的接口,比如 BeanNameAware 等。

I have seen in many libraries like Spring which use a lot of interfaces with single methods in them like BeanNameAware, etc.

实施者类将用单一方法实现许多接口。

And the implementer class will implement many interfaces with single methods.

在什么情况下保持单个方法接口是有意义的?这样做是为了避免使单个界面变得笨重,例如 ResultSet ?或者是否有一些设计标准主张使用这些类型的接口?

In what scenarios does it make sense to keep single method interfaces? Is it done to avoid making one single interface bulky for example ResultSet? Or is there some design standard which advocates the use of these type of interfaces?

推荐答案

使用Java 8,保留单一方法接口非常有用,因为单个方法接口将允许使用闭包和函数指针。因此,只要您的代码是针对单个方法接口编写的,客户端代码就可以提交闭包或方法(必须与单个方法接口中声明的方法具有兼容的签名),而不必创建匿名类。相反,如果您使用多个方法创建一个接口,则客户端代码将不具备这种可能性。它必须始终使用一个实现接口所有方法的类。

With Java 8, keeping the single method interface is quite useful, since single method interfaces will allow the usage of closures and "function pointers". So, whenever your code is written against a single method interface, the client code may hand in a closure or a method (which must have a compatible signature to the method declared in the single method interface) instead of having to create an anonymous class. In contrast, if you make one interface with more than one method, the client code will not have that possibility. It must always use a class that implements all methods of the interface.

因此,作为一个通用指南,可以说:如果一个类只暴露一个方法到客户端代码可能对某些客户端有用,然后对该方法使用单个方法接口是个好主意。一个反例就是 Iterator 界面:在这里,仅使用 next()方法没有用处没有 hasNext()方法。由于只有一个只实现其中一种方法的类是没用的,所以在这里拆分这个接口并不是一个好主意。

So as a common guideline, one can say: If a class that only exposes a single method to the client code might be useful to some client, then using a single method interface for that method is a good idea. A counter example to this is the Iterator interface: Here, it would not be useful having only a next() method without a hasNext() method. Since having a class that only implements one of these methods is no use, splitting this interface is not a good idea here.

示例:

interface SingleMethod{ //The single method interface
    void foo(int i);
}

class X implements SingleMethod { //A class implementing it (and probably other ones)
    void foo(int i){...}
}

class Y { //An unrelated class that has methods with matching signature
    void bar(int i){...}
    static void bar2(int i){...}
}

class Framework{ // A framework that uses the interface
    //Takes a single method object and does something with it
    //(probably invoking the method)
    void consume(SingleMethod m){...}
}

class Client{ //Client code that uses the framework
    Framework f = ...;
    X x = new X();
    Y y = new Y();
    f.consume(x); //Fine, also in Java 7

    //Java 8
    //ALL these calls are only possible since SingleMethod has only ONE method!
    f.consume(y::bar); //Simply hand in a method. Object y is bound implicitly
    f.consume(Y::bar2); //Static methods are fine, too
    f.consume(i -> { System.out.println(i); }) //lambda expression. Super concise.

    //This is the only way if the interface has MORE THAN ONE method:
    //Calling Y.bar2 Without that closure stuff (super verbose)
    f.consume(new SingleMethod(){
         @Override void foo(int i){ Y.bar2(i); }
    });
}

这篇关于何时在Java中使用单方法接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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