“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

digression

Saying it was a sunny morning, got the take-away little elder brother sent me the most beloved malatang, I opened the because yesterday just filling the three hair money electricity and restore the use of computer, boarded the is full of beauty friends WeChat, immediately familiar voice echoed in my this three hundred – square – meter in the bedroom, the charm of sure enough I can’t let a person, it opens at the glittering beauty face, A line of text greeted, return money, do not return money at the end of the month you malatang hot… It’s nice to say that it’s snowing all over the place, and it’s getting colder. It’s really cold

Digress… Without further ado, load up

Principle of event System

The event system can be divided into two parts, the occurrence part and the handling part. For example, if a scandal happens to a star, public relations, legal affairs and other departments will immediately respond and deal with it. An event system has the following features: 1. A party that can implement events can register corresponding events according to the event ID or name. 2. The event initiator notifies the registrants based on the registration information. 3. An event can have multiple implementers responding to it.

Event registration implementation

The handler is notified when an event occurs, so we need to connect the event to the handler, so we need to register the event to the handler

// Create a global event set, store it as a map slice, name it key, and handle it as value
var nameOfEvent = make(map[string] []func(interface{}))

// Register the event, providing the event name and the callback function
func RegisterEvent(name string, callback func(interface{})) {
    // Find the list of events by name
    list := nameOfEvent[name]
    // Add a function to the list slice
    list = append(list, callback)
    // Slice back the modified event list
    nameOfEvent[name] = list
}

Copy the code

1. Create a global event set, store it as a map slice, name it as key, and process it as value 2. Since an event can have more than one handler, use arrays to store different handlers

Event invocation implementation

// Call the event
func CallEvent(name string, param interface{}) {
    // Find the list of events by name
    list := nameOfEvent[name]
    // Iterate over all callbacks to this event
    for _, callback := range list {
        // Call the callback with an passed argument
        callback(param)
    }
}
Copy the code

2. Traverse the handler, passing in event parameters for each function callback and calling, triggering the logical processing of the event implementer.

Handler implementation

// Declare the structure of the organization
type Actor struct {
    Aname string 
    Bname string 
}
// Add an event handler for the organization
func (actor *Actor) OnEvent(param interface{}) {
    fmt.Println(actor.Aname,param)
}
// Add another event handler for the organization
func (actor *Actor) GlobalEvent(param interface{}) {
    fmt.Println(actor.Bname, param)
}

// Add another event handler for the third party organization
func ThreeOnEvent(param interface{}) {
    fmt.Println("Third Party", param)
}
Copy the code

1. First declare institutions and third parties, representing multiple processors 2. Implement corresponding processing methods

Events trigger

func main(a) {
    // Instantiate the mechanism
    actor := Actor{
       "Justice"."Public relations",}// Register a legal callback named OnSkill
    RegisterEvent("XXX cheated on me", actor.OnEvent)
     // Register a PR callback called OnSkill
    RegisterEvent("XXX cheated on me", actor.GlobalEvent)
    // Register third party events on OnSkill again
    RegisterEvent("XXX cheated on me", ThreeOnEvent)
    // All registered functions with the same name will be called
    CallEvent("XXX cheated on me"."Get going")}Copy the code

  1. Instantiate the organization and sign the departments in the organization

2. Register the event. 3

You thought it was over

As a result, role and global events are triggered sequentially in the order of registration.

In general, event systems do not guarantee the order of calls in multiple function lists of the same event implementer, and they assume that all implementation functions are equal. That is, it doesn’t matter who gets called first, whether the actor.onEvent or GlobalEvent() function in the example is registered first, and developers shouldn’t be concerned or required to guarantee the order of calls.

A well-developed event system also provides a way to remove individual and all events.

After reading, you find any mistakes, write them down below! Rub it in with my Black Tiger Fu!