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.
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?
An interface with only one abstract method is called a functional interface, or a Single Abstract Method (SAM) interface. The functional interface can have several non-abstract members but only one abstract member. To declare a functional interface in Kotlin, use the fun modifier.
In Java, an interface specifies the behavior of a class by providing an abstract type. As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported through this technology. Interfaces are used in Java to achieve abstraction.
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface.
The interfaces with only one method are called Single Abstract Method(SAM) Interfaces. With the advent of Java8, such interfaces are also called Functional Interfaces.
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.
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.
Example:
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); } }); }
Interfaces with only one (or few) methods is the key to the highly useful Strategy pattern, which is "some design standard which advocates the use of these type of interfaces".
Another common scenario is when you want a callback. Foo calls Bar as an asynchronous task, and when Bar is finished with something, the result is sent back to Foo using a callback -- which can be an interface containing only one method. (An example of this is the many listeners in Android, Event Listeners in Swing...)
Also, if you have two classes that are tightly coupled with one another (let's call them Foo and Bar). Foo uses nearly all of Bar's methods, but Bar only needs some a few of those from Foo. Foo can implement FooInterface
which is then sent to Bar. Now the coupling is looser, because Bar only knows about the FooInterface, but does not care about the other methods the implementing class contains.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With