Java实现事件机制

java中的事件机制的参与者有3种角色:

1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中

2.event source:具体的事件源,比如说,你点击一个button,那么button就是event source,要想使button对某些事件进行响应,你就需要注册特定的listener。

3.event listener:对每个明确的事件的发生,都相应地定义一个明确的Java方法。这些方法都集中定义在事件监听者(EventListener)接口中,这个接口要继承 java.util.EventListener。 实现了事件监听者接口中一些或全部方法的类就是事件监听者。
 
事件状态对象:
public class EventState extends EventObject {
 
       private static final long serialVersionUID = 1331423214321432154L;
 
       //参数为事件源和状态名
       public EventState(Object source ,String state ) {
             super ( source );
             this . state = state ;
      }
      
       private String state = "" ;
       public String getState() {
             return state ;
      }
 
 
       public void setState(String state ) {
             this . state = state ;
      }
}
 
事件监听者(事件处理)
//事件处理器 EventListener
public abstract   class Handler implements EventListener {
      
       public Handler(){}
      
       public void doHandler(EventState state ) {
            System. out .println( "handler1 执行了,现在状态是:" );
      }
}
 
事件源(事件的发起和控制)
 
       //事件源
       public class EventSource {
             //事件队列
             private Set<Handler> eventListeners ;
            
             public EventSource(){
                   this . eventListeners = new LinkedHashSet<Handler>();
            }
            
             public void addListeners(Handler handler ){
                   this . eventListeners .add( handler );
            }
            
             public void removeListeners(Handler handler ){
                   this . eventListeners .remove( handler );
            }
             //事件处理
             public void triggerEvent1(){
                   if ( eventListeners == null ) {
                         return ;
                  }
                   //改变事件状态
                  EventState state = new EventState( this , "triggerEvent1 sate" );
                  notifies( state );
            }
             //事件执行
             protected void notifies(EventState sate ){
                   if ( eventListeners .size()>0) {
                         for (Handler handler : eventListeners ) {
                               handler .doHandler( sate );
                        }
                  }
            }
      }
 
当我们触发这个事件时
       public static void main(String[] args ) {
                  EventSource source = new EventSource();
                   source .addListeners( new Handler(){
                         //重写事件处理函数
                         @Override
                         public void doHandler(EventState state ) {
                              System. out .println( "handler1 执行了,现在状态是:" + state .toString());
                        }
                  });
                  
                   source .triggerEvent1();
      
            }
 
扩展:java8中的函数回调: http://www.infoq.com/cn/articles/Functional-Style-Callbacks-Using-CompletableFuture?utm_campaign=rightbar_v2&utm_source=infoq&utm_medium=articles_link&utm_content=link_text

你可能感兴趣的:(Java实现事件机制)