Users who have used C ++ for Object-Oriented Programming know that objects in programs rarely exist independently. It is almost impossible to ignore the interaction between objects. Therefore, it is an important task of object-oriented programming to identify the relationship between objects or establish a message connection between objects. This paper proposes a practical method for establishing message connections between objects from the perspective of C ++ program design. For more information about object-oriented programming technology, see related monographs.
We all know that objects are the encapsulation bodies of data and methods. In C ++, they are data members and member functions. The program designer changes the object state by executing various methods of the object (that is, changing the attribute data of the object ). In this way, some "events" occur to this object ". When an object has an event, it usually needs to send a "message" to other related objects and request them for some processing. At this time, the object that occurs and requests to other objects is called the "event object", and the object that handles the event is called the "Callback object ". A callback object is called a callback function to process events ".
In C ++, this process is equivalent to calling some member functions of the callback object when an event occurs. Generally, the callback object transmits the object pointer to the event object. However, this method is not universal. In order to reduce the workload of program design, this paper proposes a system method for establishing message connections between objects. The idea is to abstract the process "event occurrence> request processing> Execution Processing" into a "CallBack" class. With inheritance, you can easily obtain the mechanism for establishing message connections between objects.
I. Data Structure and member functions of the callback class
The CallBack class proposed in this article supports three types of CallBack functions. They are member functions in the callback object, static member functions and common C functions in the callback class. The CallBackle class contains a callback function table callBackList. It is used to record the event name and point to the callback function and the pointer to the callback object. Each node in the table is an event record EventRecord. Each event record contains three domains: The event name pointer eventName, the pointer to the callback object pointerToCBO, And the pointer to the callback function pointerToCBSF or pointerToCBSF (wherein, pointertocvr points to the member function of the callback object, pointerToCBSF points to the static member function or common function of the callback class. ). The CallBack mechanism provided by the CallBack class is as follows: register the CallBack function in the CallBack object on the event object; when an event occurs, the event object retrieves and executes the CallBack function in its CallBack table. So that the message connection between the two can be established. (For the specific implementation of this class, see the program list attached to the article) callback object
Event object
Event name callback Object Pointer callback function pointer
"Event" pointerCBO pointertocbr or
PointerTOCBSF
------ |
AddCallBack: registers the event name and the pointer to the callback function and the callback object.
CallCallBack: In the callback table, retrieve the callback functions registered on the specified event and call them.
Call the CallCallBack function when an event occurs.
Member functions that process event
CallBackList inherited from the CallBack class, which includes the member functions AddCallBack and CallBack.
When the callback function is a static member function or a common C function, pointerToCBO is NULL.
The event name is the retrieval keyword in the callback table callBackLis.
Other member functions in the callback object
The member function AddCallBack of the CallBack class is used to register the CallBack function to the CallBack table of the event object. It has two overloaded versions:
Void CallBack: AddCallBack (char * event, CallBackFunction cbf, CallBack * p ); Void CallBack: AddCallBack (char * event, CallBackStaticFunction cbsf ); |
The first AddCallBack is used to register the member functions of a callback object to the callback table of the event object. The second AddCallBack is used to register static member functions of a callback class to the callback table of the event object. In the preceding parameter table, event is the pointer to the event name string, p is the pointer to the callback object, and cbsf is the pointer to the member function and static member function (or common function) respectively) pointer. When a callback function comes from a callback object SomeObject, the format of passing the member function pointer should be as follows:
(CallBackFunction) & SomeObject: MemberFunctionName; a static member function pointer passing the SomeObject class should adopt the format: (CallBackStaticFunction) & SomeObject: FunctionName; when passing common function pointers in a program, you only need to pass the function name. |
The CallBack class member function void CallBack: CallCallBack (char * ename, CallData calldata = NULL) is used to call all the CallBack functions registered on the event ename. Among them, calldata is the Data Pointer (CallData is actually void *, see the program list ). The event object can pass useful data to the callback object. This member function is usually called in the member function of the event object, because only the member function of the event object can change the internal data of the object, so that some events occur.
The member function RemoveCallback is used to delete the callback function registered on the event object. The three overloaded versions are as follows:
Void CallBack: RemoveCallBack (char * event, CallBackFunction cbf, CallBack * p );
Void CallBack: RemoveCallBack (char * event, CallBackStaticFunction cbsf );
Void CallBack: RemoveCallBack (char * event ); |
Among them, parameters such as event, cbsf, and p are the same as those in the member function AddCallBack. The first RemoveCallBack is used to delete a member function registered with a callback object on the event. The second RemoveCallBack is used to delete a common function registered on the event or a static member function of a callback class. The third RemoveCallBack is used to delete all callback functions registered on the event.
Ii. usage of the CallBack class
To use the CallBack class, follow these steps:
1. Determine which objects in the program have a relationship and establish a message connection. Determine which object is the event object and which is the callback object in the connection relationship of each specific message.
2. Both the event object class and the CallBack object class must inherit from the CallBack class to obtain CallBack support.
3. Register callback data for the event object. Including the event name, callback function name, and pointer to the callback object.
4. When an event you are interested in occurs, call the CallCallBack function in the member function that triggers the event in the event object class.
The following is a specific example. You will have a better understanding of the usage of the Callback class.
File: // test program file: test. cpp
# Include "callback. h"
File: // "Speaker" Class
Class Speaker: public CallBack { Private: Int volume; Public: Speaker (int v): volume (v ){} Void IncreaseVolume (int v) file: // adds the volume member function. { Volume + = v; If (volume> 20) {file: // "volume greater than 20" event occurred File: // call the callback function registered on two events CallCallBack ("volume changed "); CallCallBack ("volume greater than 20", & volume ); } }
Void DecreaseVolume (int v) file: // The volume reduction function. { Volume-= v; If (volume <5) {file: // "volume less than 5" event occurred File: // call the callback function registered on two events CallCallBack ("volume changed "); CallCallBack ("volume less than 5", & volume ); } } };
File: // "ears" Class
Class Ear: public CallBack { Public: Static void Response (CallData callData) file: // Response to "volume change" { Cout <"the volume has changed." < } Void HighVoiceResponse (CallData callData) // high-pitched response { Cout <"hello! Too noisy! The current volume is: "<* (int *) callData) < } Void LowVoiceResponse (CallData callData) // response to the bass { Cout <"Ah! I cannot hear it. The current volume is: "<* (int *) callData) < } }; Void main (void) { Speaker s (10); file: // The current volume is 10 Ear e; File: // registers the callback function for event object s S. AddCallBack ("volume greater than 20", (CallBackFunction) & Ear: HighVoiceResponse, & e ); S. AddCallBack ("volume less than 5", (CallBackFunction) & Ear: LowVoiceResponse, & e ); S. AddCallBack ("volume changed", (CallBackStaticFunction) & Ear: Response ); S. IncreaseVolume (12); // increase the volume by 12. The current volume is 22 S. DecreaseVolume (20); // reduce the volume by 20. Now the volume is 2 } |
Running result:
The volume has changed.
Hello! Too noisy! The current volume is 22.
The volume has changed.
Ah! I cannot hear it. The current volume is: 2
In the preceding example, the speaker object s is the event object, and the ear Object e is the callback object .. Three events are registered on s: "volume changed", "volume greater than 20", and "volume less than 5 ". The callback functions are: Ear: Response, Ear: HighVoiceResponse, and Ear: LowVoiceResponse. When the speaker s uses its member functions IncreaseVolume and Decrease