functional programming - What are the uses of constructor reference in java 8 -
i reading java 8 features, lead me article , wondering actual uses of constructor reference, mean why not use new obj
?
p.s, tried googling, failed find meaningful, if has code example, link or tut great
first of all, should understand constructor references special form of method references. point method references not invoke referenced method provide way define function invoke method when being evaluated.
the linked article’s examples might not useful that’s general problem of short self-contained example code. it’s same “hello world” program. it’s not more useful typing text “hello world” directly console it’s not meant anyway. it’s purpose demonstrate programming language.
as assylias has shown, there use cases involving existing functional interface
s using jfc api.
regarding usefulness of custom functional interface that’ll used constructor reference, have think reason use (functional) interface
in general: abstraction.
since purpose of interface
abstract underlying operation, use cases places not want perform unconditional new sometype(…)
operation.
so 1 example commonly known factory pattern define interface
construct object , implementing factory via constructor reference one option out of infinite possibilities.
another important point kinds of generic methods possibility construct instances of type, not known due type erasure, needed. can implemented via function passed parameter , whether 1 of existing functional interface
s fits or custom 1 needed depends on required number , types of parameters.
Comments
Post a Comment