Advertisement
wingman007

Java2014_NestedLocalAnonymousLambdaEvents

Dec 15th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package nestedlocalanonymouslambdaevents;
  7.  
  8. /**
  9.  *
  10.  * @author Kristiqn
  11.  */
  12. public class NestedLocalAnonymousLambdaEvents {
  13.  
  14.     private static class NestedListener implements MyEventHandler{
  15.         @Override
  16.         public void happend(Object sender) {
  17.             System.out.println("Something happaned.Event handler in my Nested class.");
  18.         }
  19.     }
  20.    
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         // 0. conventional approach with a class
  26.         Notifier notifier = new Notifier();
  27.         notifier.doSomething();        
  28.         notifier.onSomething = new Listener();        
  29.         notifier.doSomething();
  30.        
  31.         // 1. Nested class
  32.         notifier.onSomething = new NestedListener();  
  33.         notifier.doSomething();
  34.        
  35.         // 2. Local class
  36.         class LocalListener implements MyEventHandler {
  37.             @Override
  38.             public void happend(Object sender) {
  39.                 System.out.println("Something happaned.Event handler in my Local class.");
  40.             }
  41.         }
  42.         notifier.onSomething = new LocalListener();  
  43.         notifier.doSomething();
  44.        
  45.         // 3. Anonymous class
  46.         notifier.onSomething = new MyEventHandler() {
  47.             @Override
  48.             public void happend(Object sender) {
  49.                 System.out.println("Somethign happaned. Event handler in my anonymous class.");
  50.             }
  51.         };
  52.         notifier.doSomething();
  53.        
  54.         // 4. Lambda
  55.         notifier.onSomething =
  56.                 (Object object) -> System.out.println("Somethign happaned. Event handler lambda expression");
  57.         notifier.doSomething();
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement