C ++ Program Design establishes a message connection between objects (with a program list) (1)

Source: Internet
Author: User

UsedC ++TargetedObjectUsers of programming know that objects in a program 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)

Event name Callback object pointer Callback function pointer
"Event" PointerCBO PointerTOCBSF 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:

 
 
  1. void CallBack::AddCallBack(char *event,CallBackFunction cbf,CallBack *p);   
  2. 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 passed member function pointer should adopt the following format: (CallBackFunction) & SomeObject: MemberFunctionName; when passing a static member function pointer of the SomeObject class, the format should be: (CallBackStaticFunction) & SomeObject: FunctionName; when passing a common function pointer 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:

 
 
  1. void CallBack::RemoveCallBack(char *event,CallBackFunction cbf,CallBack *p);   
  2. void CallBack::RemoveCallBack(char *event,CallBackStaticFunction cbsf);   
  3. 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.


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.