实现简单的自发自收的EventDispatcher

下午自己写了个简单的实现demo不是很完善,贴出来与大家分享。这是一个简单的demo,实现了发送接收,参数携带等基本功能。没有实现冒泡等复杂的东东,有兴趣的可以自己继续扩展实现。

  
    
1 package
2 {
3 import com.zs.event.Event;
4 import com.zs.event.EventDispatcher;
5
6 import flash.display.Sprite;
7
8 public class MyEventDispatcher extends Sprite
9 {
10 public function MyEventDispatcher()
11 {
12 var obj:EventDispatcher = new EventDispatcher();
13 obj.addEventListener( " HAHA " ,callBackFun);
14 var event:Event = new Event( " HAHA " );
15 event.parameter = " my name is zhengs " ;
16 obj.dispatcher(event);
17 }
18
19 private function callBackFun(event:Event): void
20 {
21 trace(event.parameter);
22 }
23 }
24 }

 

  
    
1 package com.zs.event
2 {
3 public class Event extends Object
4 {
5 public var type:String;
6
7 public var parameter:Object;
8
9 public function Event(type:String)
10 {
11 this .type = type;
12 }
13 }
14 }

 

  
    
1 package com.zs.event
2 {
3 public class EventDispatcher extends Object
4 {
5 private var obj:Object = new Object();
6
7 public function EventDispatcher()
8 {
9
10 }
11
12 public function addEventListener(event:String,callBackFun:Function): void
13 {
14 obj[event] = callBackFun;
15 }
16
17 public function dispatcher(event:Event): void
18 {
19 obj[event.type](event);
20 }
21 }
22 }

 

你可能感兴趣的:(dispatcher)