Overview
Features
Download
Documentation
Community
Add-Ons & Services

Registering an observer for notifications from other classes

A general discussion forum.

Registering an observer for notifications from other classes

Postby harpreet » 21 Jan 2013, 04:28

I am using Poco to send notifications to from one class to another. I have three classes in my application.

Code: Select all
    class MainClass{
    public:
    method1(){//postNotification so that only handleMethod1 of ShapeClass is called}
    method2(){//postNotification so that only handleMethod2 of ShapeClass is called}
    private:
    NotificationCenter nCenter;
    };

    //passed a reference of the notification center
    class ShapeClass1{
    ShapeClass1(NotificationCenter* nc){
    Observer<ShapeClass, Notification> o(*this, &ShapeClass::handleNotificationfromMainClassMethod1);
    Observer<ShapeClass, Notification> o1(*this, &ShapeClass::handleNotificationfromMainClassMethod2);
    }
    handleNotificationfromMainClassMethod1(Notification* nf1){...}
    handleNotificationfromMainClassMethod2(Notificatino* nf2){...}
    method3(); // post notification
    };

     //passed a reference of the notification center
        class SystemClass1{
    SystemClass1(NotificationCenter* nc){
    }
        handleNotificationfromShapeClass1()
        };

I have to make the observers in ShapeClass1 to subscribe to the notifications frmo MainClass. In additon the main concern here is how to post notifications so that only observers fitting a certain condition are called. For eg: from main class 1, when I post notification from method1(), I want only the observer o to call the handler methods and o1 should ignore the notification. How do I post notification to matching observers only? And how do I register observers to listen to notifications from classes other than themselves?

Poco Notification Center [url="http://www.appinf.com/docs/poco/Poco.NotificationCenter.html"]example here[/url]
harpreet
 
Posts: 3
Joined: 15 Jan 2013, 15:05

Re: Registering an observer for notifications from other cla

Postby alex » 21 Jan 2013, 04:46

If you follow the link I posted as response to your first post, you'll find this code that has two notifications and two handlers:

Code: Select all
 void NotificationCenterTest::test5()
{
NotificationCenter nc;
nc.addObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
nc.addObserver(Observer<NotificationCenterTest, TestNotification>(*this, &NotificationCenterTest::handleTest));
//...
nc.postNotification(new Notification);
nc.postNotification(new TestNotification);
//...
nc.removeObserver(Observer<NotificationCenterTest, Notification>(*this, &NotificationCenterTest::handle1));
nc.removeObserver(Observer<NotificationCenterTest, TestNotification>(*this, &NotificationCenterTest::handleTest));
}
alex
 
Posts: 1145
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Registering an observer for notifications from other cla

Postby harpreet » 21 Jan 2013, 11:59

Just to follow up to the above response, I wanted to ask if posting notifications of different types will mean that I'll have to create new classes for each type of notification?

For eg:
ShapeClass has to post two notifications of two different types which have to be observed in FountainClass (create 2 classes for this?)
FountainClass has to post two notifications of two different types which have to be observed in LightClass (create 2 more classes for this?) and so on.
So, in essence these notification classes should be in a separate file which can be included in classes such as FountainClass, LightClass, ShaoeClass ?

If the answer to above is true, how is sending the notifications different from sending the eventargs on the other hand?
harpreet
 
Posts: 3
Joined: 15 Jan 2013, 15:05


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests