First of all, I also experienced "think the callback function is better than the event model" and "think the event model is better than the callback function". These two stages have passed before. In fact, the "callback function" and "event model" can be two indicators of the architecture. Here, I just want to talk about their advantages and disadvantages.
Callback Function
Advantages:
1. No Memory leakage
2. Simple Writing. The number and meaning of callback functions in a method are fixed, so you do not need to look for the desired callback function name.
3. You cannot add two callback functions to the same content to avoid repeated addition.
4. The parameter list of the callback function is flexible and can be written directly, and it is fixed. If an error is written, an error will be reported during execution to facilitate troubleshooting.
5. The callback function and its parameters are fixed. If the callback function is missing, the callback function will not work. If the callback function is different from the callback function, you can know it immediately to facilitate collaboration.
Disadvantages
1. The callback function cannot be referenced. It may be recycled before it is executed, and the reliability is low.
2. Each time you add content, you must modify the parameter list of all functions. It is more difficult to add a new callback function.
3. You cannot add two callback functions to the same content, which limits the function.
4. The parameter list of the callback function is difficult to use unless you do not know the code or help (comment.
5. The callback function and its parameters are fixed, and the callback function will not work if there are more and less parameters. The callback function must follow your steps, which is not conducive to parallel development.
Event Model
Advantages:
1. improper use may cause memory leakage.
2. events can be easily modified. Add new events, modify data types, and use them in different places.
3. You may listen to two event functions for the same event, and the event can be passed and so on. It has many convenient features (such as the bubble of the display object)
4. events, whether of the event type or the data type passed by the event, can be detected during compilation and are easy to use with code prompts.
5. Events are free to send even if there is no receiver, which is suitable for parallel development.
Disadvantages:
1. As long as strong references are used, functions can be executed in the end, and the reliability is high (of course, some special cases such as javasdobject are not enough)
2. The data types that can be passed by each event type are fixed. to adapt to new situations, adding new events will add a lot of code. In addition, you also need to remove and so on. To set properties, you also need separate code.
3. You may accidentally listen to two event functions for the same event, resulting in repeated calls. In addition, events may be triggered when they are not needed.
4. When the event type reaches a certain level, you do not know which event type to choose to locate the specific event you want to send. Therefore, it is impossible to correspond to an event type in one case. However, if it is easy to use objects for generalization, its original advantages will be lost.
5. events can be sent even if they are not accepted. Therefore, you do not know whether the recipient has successfully accepted the event.
Compare the values of 1, 2, 3, 4, and 5 in four parts to show what I mean.
I'm not sure. That is, "view the situation. Of course, if only one event is required, select the event.
But simply put, if anyone is good, is there anything you really want to consider at hand? The program cannot be empty talk, let alone B.