1. Notification. cpp:
# Include <poco/icationicationcenter. h>
# Include <poco/notification. h>
# Include <poco/observer. h>
# Include <poco/nobserver. h>
# Include <poco/autoptr. h>
# Include <iostream>
Using poco: icationicationcenter;
Using poco: notification;
Using poco: observer;
Using poco: nobserver;
Using poco: autoptr;
Class basenotification: public notification {
Public:
Basenotification (){
STD: cout <"construct basenotification" <STD: Endl;
}
~ Basenotification (){
STD: cout <"destruct basenotification" <STD: Endl;
}
};
Class subnotification: Public basenotification {
Public:
Subnotification (){
STD: cout <"construct subnotification" <STD: Endl;
}
~ Subnotification (){
STD: cout <"destruct subnotification" <STD: Endl;
}
};
Class target {
Public:
Void handlebase (basenotification * PNF ){
STD: cout <"handlebase:" <PNF-> name () <STD: Endl;
PNF-> release ();
}
Void handlesub (const autoptr <subnotification> & PNF ){
STD: cout <"handlesub:" <PNF-> name () <STD: Endl;
}
};
Int main (INT argc, char ** argv ){
Icationicationcenter NC;
Target target;
NC. addobserver (
Observer <target, basenotification> (target, & target: handlebase)
);
NC. addobserver (
Nobserver <target, subnotification> (target, & target: handlesub)
);
NC. postnotification (New basenotification );
NC. postnotification (New subnotification );
NC. removeobserver (
Observer <target, basenotification> (target, & target: handlebase)
);
NC. removeobserver (
Nobserver <target, subnotification> (target, & target: handlesub)
);
Return 0;
}
Ii. Summary of key points
1. Notification communicates with target through Observer
2. The observer stores two pointers, one pointing to the target and the other pointing to the callback function. For example, target: handlebase is the callback function. In addition, the observer knows which notification the target is interested in.
3. Target subscribes to notifications of interest through the addobserver () method of icationicationcenter. You can also use removeobserver () to cancel the subscription of related notifications.
4. observer is used together with common pointers, while nobserver is used together with smart pointers. When using common pointers, you need to release the memory in the taiget destructor. Otherwise, the memory may leak, smart pointers are not required.
5. Define callback functions in two ways
Method 1: void somecallback (somenotification * PNF); // normal pointer Mode
Method 2: void somecallback (const autoptr <somenotification> & PNF); // smart pointer Mode
6. The delivery notification is sent by the icationicationcenter postnotification () method, and the function prototype is
Void postnotification (Notification: PTR pnotification); this method will send a notification to all target
PS: I wrote an article at the beginning. Please forgive me. If you have any questions or communication, you can add your YY: 301558660
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8053886