Java Functional Programming
Java Functional Programming
Filter function:
Functions are usually used when the delegation is simple and doesn't require
complex logic or state management. For example, if you need to delegate a simple
arithmetic operation, like adding two numbers, a function would be a good choice. In
this case, you could define a function that takes two parameters and returns their
sum, and then delegate the operation to this function.
On the other hand, if the delegation requires more complex logic or state
management, classes are usually a better choice. For example, if you need to
delegate a task that involves multiple steps or requires the use of multiple objects, a
class would be more appropriate. In this case, you could define a class that
encapsulates the logic and state needed to perform the task, and then delegate the
task to an instance of this class.
In general, the choice between functions and classes in the delegate pattern depends
on the specific requirements of the application. If the delegation is simple and
doesn't require much logic or state management, functions can be used. If the
delegation is more complex and requires more logic or state management, classes
are a better choice.
Note : This is one way of converting static methods to functions that can be
injected into other classes ‘constructors. This allows us to use dependency
injection principle even with static methods, thereby improving the
testability and portability of the class(fewer dependencies).
Functions are usually used to implement the Decorator pattern when the
behavior modification is simple and doesn't require complex state
management or object instantiation. For example, if you need to add a
simple logging functionality to a method, a function can be defined that
wraps around the original method and logs the input and output.