Golang realizes simple and easy to use Eventdispatcher

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Events Source Code

Package Ax_eventsimport "FMT"//Event Type base class type event struct {//Event trigger Instance Target Ieventdispatcher//Event Type Strin G//Events carry data source Object interface{}}//Event Scheduler base class type Eventdispatcher struct {Savers []*eventsaver}//Event Scheduler Unit type Ev entsaver struct {type string Listeners []*eventlistener}//listener Type eventlistener struct {Handler Eventhand ler}//listener function Type EventHandler func (Event event)//Events Dispatch interface type Ieventdispatcher interface {//Event listener AddEventListener (EV Enttype string, listener *eventlistener)//Remove Event listener RemoveEventListener (EventType string, listener *eventlistener) Boo L//Whether it contains event Haseventlistener (EventType string) bool//Event dispatch dispatchevent bool}//Create event dispatcher func Newev Entdispatcher () *eventdispatcher {return new (Eventdispatcher)}//Create listener func neweventlistener (H EventHandler) *eventlis  tener {L: = new (EventListener) L.handler = h return l}//Create event func newevent (EventType string, Object interface{}) Event {e: = Event{tyPe:eventtype, Object:object} return e}//Clone event func (this *event) clone () *event {e: = new (event) E.type = this. Type e.target = e.target return E}func (this *event) ToString () string {return FMT. Sprintf ("Event Type%v", this.) Type)}//Event Scheduler Add event func (this *eventdispatcher) AddEventListener (EventType string, listener *eventlistener) {for _, save R: = Range This.savers {if saver. Type = = EventType {Saver. Listeners = Append (saver. Listeners, Listener) return}} saver: = &eventsaver{type:eventtype, Listeners:[]*eventlistene R{listener}} this.savers = Append (this.savers, Saver)}//Event Scheduler removes a listener func (this *eventdispatcher) RemoveEventListener ( EventType string, listener *eventlistener) bool {for _, saver: = Range This.savers {if saver. Type = = EventType {For I, L: = Range saver. Listeners {if listener = = L {Saver. Listeners = Append (saver. LISTENERS[:I], saver. Listeners[i + 1:]...) Return True}}}} return false}//Event Scheduler contains a type of listener func (this *eventdispatcher) have EventListener (EventType string) bool {for _, saver: = Range This.savers {if saver. Type = = EventType {return true}} return false}//Event Scheduler Dispatch event func (this *eventdispatcher) Dispatche Vent (Event event) bool {for _, saver: = Range This.savers {if saver. Type = = event. Type {for _, Listener: = Range saver. Listeners {event. Target = This listener. Handler (Event)} return True}} return False}

Events contains several core classes:
Event
Eventdispatcher
EventListener
EventHandler
Eventsaver
Interface
Ieventdispatcher
Examples of use are as follows:

package mainimport (    "fmt"    e "alex/ax_events"    "time")const HELLO_WORLD = "helloWorld"func main() {    dispatcher := e.NewEventDispatcher()    listener := e.NewEventListener(myEventListener)    dispatcher.AddEventListener(HELLO_WORLD, listener)    time.Sleep(time.Second * 2)    //dispatcher.RemoveEventListener(HELLO_WORLD, listener)    dispatcher.DispatchEvent(e.NewEvent(HELLO_WORLD, nil))}func myEventListener(event e.Event) {    fmt.Println(event.Type, event.Object, event.Target)}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.