https://github.com/njniecong/JavaDemo/tree/master/java/demo/desgin_pattern/listener/demo1

 1 package demo.desgin_pattern.listener.demo1;
 2 
 3 /**
 4  * @author niecong
 5  * @创建时间 2019/8/10
 6  * 事件源持有 一个监听器  并且实现了事件的接口
 7  **/
 8 public class EventSource implements IEvent {
 9     boolean button;
10     boolean mouse;
11     private IEventListener mEventListener;
12 
13     //注册监听器
14     @Override
15     public void setEventListener(IEventListener arg) {
16         mEventListener = arg;
17     }
18 
19     //触发事件
20     public void mouseEventHappened() {
21         mouse = true;
22         mEventListener.doEvent(this);
23     }
24 
25     @Override
26     public boolean ClickButton() {
27         return button;
28     }
29 
30     @Override
31     public boolean MoveMouse() {
32         // TODO Auto-generated method stub
33         return mouse;
34     }
35 
36 }
 1 package demo.desgin_pattern.listener.demo1;
 2 
 3 /**
 4  * @author niecong
 5  * @创建时间 2019/8/10
 6  **/
 7 public class EventSource2 implements IEvent {
 8     boolean button;
 9     boolean mouse;
10     private IEventListener ml;
11 
12     @Override
13     public void setEventListener(IEventListener arg) {
14         ml = arg;
15     }
16 
17     @Override
18     public boolean ClickButton() {
19         // TODO Auto-generated method stub
20         return button;
21     }
22 
23     @Override
24     public boolean MoveMouse() {
25         // TODO Auto-generated method stub
26         return mouse;
27     }
28 
29     // 触发事件
30     public void buttonEventHappened() {
31         button = true;
32         ml.doEvent(this);
33     }
34 
35 }
 1 package demo.desgin_pattern.listener.demo1;
 2 
 3 /**
 4  * @author niecong
 5  * @创建时间 2019/8/10
 6  **/
 7 public interface IEvent {
 8     void setEventListener(IEventListener arg);
 9 
10     boolean ClickButton();
11 
12     boolean MoveMouse();
13 
14 }
 1 package demo.desgin_pattern.listener.demo1;
 2 
 3 /**
 4  * @author niecong
 5  * @创建时间 2019/8/10
 6  * 事件监听器的 方法是 事件处理器
 7  **/
 8 public interface IEventListener {
 9 
10     void doEvent(IEvent arg);
11 
12 }
 1 package demo.desgin_pattern.listener.demo1;
 2 
 3 /**
 4  * @author niecong
 5  * @创建时间 2019/8/10
 6  **/
 7 public class Test {
 8 
 9     public static void main(String[] args) {
10         // 事件源(被监听的对象)
11         EventSource m1 = new EventSource();
12         EventSource2 m2 = new EventSource2();
13         // 监听器
14         IEventListener mEventListener = new IEventListener() {
15 
16             @Override
17             public void doEvent(IEvent arg) {
18                 if (true == arg.ClickButton()) {
19                     System.out.println("你点击了按钮");
20                 } else if (true == arg.MoveMouse()) {
21                     System.out.println("你移动了鼠标");
22                 }
23             }
24         };
25 
26         // lambda 写法
27 //        IEventListener mEventListener2 = (IEvent arg)->{
28 //            if (true == arg.ClickButton()) {
29 //                System.out.println("你点击了按钮");
30 //            } else if (true == arg.MoveMouse()) {
31 //                System.out.println("你移动了鼠标");
32 //            }
33 //        };
34 
35 
36 
37         // 注册监听器到事件源
38         m1.setEventListener(mEventListener);
39         m1.mouseEventHappened();
40 
41         // 注册监听器到事件源
42         m2.setEventListener(mEventListener);
43         m2.buttonEventHappened();
44     }
45 
46 }