· A functor is an object representing a function.
· As an object, it can be stored in a variable, passed as an input to a higher-order function, or returned as a value by a higher-order function.
· As a function, it can be called like any other function.
· In short, functors represent both data and the algorithms that process data.
· The concept of functor is central to the Functional Programming Paradigm.
· Functors appeared in Java 8.
· This opens the possibility of replacing class methods—which can't be changed at runtime—with functors stored in class member variables—which can be changed at runtime.
UW 6 is similar to UW 4, which used the Strategy Pattern. The difference is that instead of making developers introduce a hierarchy of strategy classes with fixed attack methods, they can simply use Java 8 lambda notation to dynamically create functors:
Here's the implementation: