1) SAM= Single Abstract Method = An Interface with Only One Method = Functional Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FunctionalInterface | |
public interface StateChangeListener { | |
public String onStateChange(State oldState, State newState); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StateManager { | |
public void addNewStateListener(StateChangeListener listener) { //blahh } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StateManager stateManager = new StateManager(); | |
stateManager.addNewStateListener( | |
(oldState, newState) -> System.out.println("State changed") | |
); |
Okeyy... I know some of you need bullshit also :)
If you want to pass a behavior to a parameter (behavior= function=action) just create an interface and annotate it @FunctionalInterface and use Lambda Syntax like i did in above.
(param1,param2,,,,,paramN) -> {play with params}
Java 8 lots of built in @FunctionalInterface inside it. You can use them anywhere you need. Just google for them .
Bye
No comments:
Post a Comment