-
Predefined generic functional interface1:
-
No parameters up to 8 parameters no return value IAction ~ IAction8Arg
-
No parameters to up to 8 parameters with return value IFunc ~ IFunc8Arg
-
-
Multicast generic functional interface3:
-
No parameters to up to 8 parameters no return value Action ~ Action8Arg
-
No parameters to up to 8 parameters with return value Func ~ Func8Arg
The multicast generic functional interface is the encapsulation of a set of predefined generic functional interfaces. Calling the invoke4 of the multicast functional interface will call the invoke of all functional interfaces in the set in turn. It supports added, removed, contains5, equals and hashCode operation, supports adding and modifying operations during invoke, multi-thread safety.
-
-
Predefine commonly used generic functional interfaces:
- Isolate the functional interface from the implementation.
- Reduce the number of classes.
-
Multicast generic functional interface:
- It is convenient for developers to program based on events6.
- Reduce collection implementation code.
-
Too many functional interfaces
Since the addition of java 8 Lambda Expressions, it is easy for developers to use lambda expressions when needed declares a new functional interface. More and more functional interfaces are declared, but except that the interface name and the method name in the interface may be different, the return value and parameter list of the method in the interface are the same. There is no need to define so many substantially identical functional interfaces, reuse the preset functional interfaces in this library, reduce the number of classes in the system, and simplify the code.
-
Iteratively write functional interface collections
Write like Observer Pattern and Publish-subscribe Pattern, you need to maintain a collection of functional interfaces to implement multicast. And this practice is very common, and if you write an implementation of a functional interface collection every time you need it, there will be many unnecessary duplicate code segments. Using the multicast generic functional interface in this library eliminates the need to repeatedly write multicast implementations.
-
Unnecessary coupling
If you declare a new functional interface specifically for a specific implementation when using a functional interface, the functional interface is coupled to the implementation-specific artifact. However this coupling is not necessary, use the functional interface in this library to decouple the functional interface from the implementation.
If you find any bugs or have any suggestions, please contact me (ryuu).
Footnotes
-
C# generics are reified generics, which can define classes with the same interface name but different generic parameters. Java generics are non-reified generics, classes with the same interface name but different generic parameters cannot be defined. For details, see Type erasure versus reified generics
Functional programming like Java Lambda expressions in .NET implementation is called delegate. Unlike Java functional interfaces, delegates in .NET default to multicast delegates, and .NET declares a variety of generic delegates ranging from no parameters to 16 parameters, from no return value to return value. Therefore, developers can use .NET's declared delegates instead of declaring their own. For details, see CSharp-Delegate, Functional programming. ↩ -
The parameter table of this functional interface is (TSender sender, TEventArgs arg), see EventHandler.java and EventArgs.java for details. ↩
-
java does not allow developers to define operator overloading, so the operation of multicast generic functional interface does not have syntactic sugar such as +, +=, - and -= delegated in C#. ↩
-
Implementing a functional interface is not calling the target method, but invokeing the target method. ↩
-
In particular, if the input of contains is a multicast, it will be judged that the current multicast is a continuous subsequence of the elements of the multicast set with the input of the input. ↩
-
Such as Observer Pattern and Publish-subscribe Pattern. ↩