One simple use for Function
s is to get properties of objects, allowing you to (among other things) create a transformed view of a collection containing a certain property from each element. For example, transforming a list of Person
objects to a String
collection of names. Any time you want to do something with a collection of objects based on a certain property of those objects, this is useful.
However, it's rather ugly and awkward to have to create an anonymous inner class inline when calling a method that uses a Function
. While you can create static final
instances of such functions and methods that return instances of functions, with many classes you could end up with many such instances and methods. One good way to reduce the number of Function
objects you need to create and make it easy to work with certain common properties (such as IDs, names, etc.) on many objects is to create interfaces that each expose a single such property:
Not only does this help add consistency to your classes, it allows you to collect behavior that can work on any type of object that has the property the interface exposes. I'd probably name such classes as the plural of the property name ("Ids" and "Names", for example).
This makes working with the IDs of a list of things with IDs as easy as possible:
The GET
function is exposed to allow its use for anything you might want a function for, not just tranform
. For example, with a HasName
interface and Names
class similar to the code for IDs, I could do something like this:
This would of course filter a list of people to an iterable that only includes people with the name Bob. It uses static imports (because they make this stuff read a lot nicer), and the methods are:
I think this approach can make working with the properties of objects using Function
s a lot easier and cleaner, while also encouraging more consistency between classes in general, which is nice.
2 comments:
Goods stuff. Makes plain old static Java looks more like dynamic language.
What casino games are there that can be played online? - Dr
Some games 의왕 출장마사지 are available on other platforms. In the list below you'll 계룡 출장안마 see 화성 출장마사지 one such game as Slots Empire. Some 서산 출장샵 of them can be 세종특별자치 출장샵 found on NetEnt.
Post a Comment