Skip to content

Ryuu-64/functional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

functional

简体中文

Codacy Badge

what is this?

  1. Predefined generic functional interface1:

    1. No parameters up to 8 parameters no return value IAction ~ IAction8Arg

    2. No parameters to up to 8 parameters with return value IFunc ~ IFunc8Arg

    3. IEventHandler2

  2. Multicast generic functional interface3:

    1. No parameters to up to 8 parameters no return value Action ~ Action8Arg

    2. No parameters to up to 8 parameters with return value Func ~ Func8Arg

    3. EventHandler

    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.

Why use?

  1. Predefine commonly used generic functional interfaces:

    1. Isolate the functional interface from the implementation.
    2. Reduce the number of classes.
  2. Multicast generic functional interface:

    1. It is convenient for developers to program based on events6.
    2. Reduce collection implementation code.

details

  1. 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.

  2. 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.

  3. 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.

about

contact me

If you find any bugs or have any suggestions, please contact me (ryuu).

Footnotes

Footnotes

  1. 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.

  2. The parameter table of this functional interface is (TSender sender, TEventArgs arg), see EventHandler.java and EventArgs.java for details.

  3. 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#.

  4. Implementing a functional interface is not calling the target method, but invokeing the target method.

  5. 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.

  6. Such as Observer Pattern and Publish-subscribe Pattern.