Poco Database Study Notes (10) poco: icationicationcenter

Source: Internet
Author: User

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

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.