Results 1 to 15 of 15

Thread: Replace the typical observer pattern to the Qt's signal and slot

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Replace the typical observer pattern to the Qt's signal and slot

    Hello forum,


    I am going through a source code where they have implemented the typical observer pattern and i need to re-implement it using the Qt's signal & slot mechanism.


    Any guideline would be very helpful


    Regards
    Sajjad

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Event source (object class creating the event should be sub-class on QObject) will emit a Qt signal, and observer (also a sub-class of QObject) will connect its slot to the emitted signal

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    In addition, it could be quite useful to declare pure virtual slot void Observer::update( Source * changedSource ) = 0; in Observer class, this way all observers will share the same interface for receiving updates.
    observer (also a sub-class of QObject) will connect its slot to the emitted signal
    Maybe its a little detail, but I'd leave management of Observers to Watched class. In typical observer pattern ("Gang of Four"), Watched object defines methods for adding and removing Observers, so creating connection could be a part of Watched::add(Observer * o) method (and Watched::remove(Observer* o) could simply call this->disconnect(o)).

  4. #4
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Hello Santosh!


    thanks for the hint. I am taking an example and let's see it helps me to understand what you tried to explain:

    Qt Code:
    1. class propertylistwidget : public networkobserver
    2. {
    3. public:
    4.  
    5. // Implementation of the ProcessorNetworkObserver interface
    6. void networkChanged();
    7. void processorAdded(const Processor* processor);
    8. void processorRemoved(const Processor* processor);
    9. void processorRenamed(const Processor* p, const std::string& prevName);
    10. void connectionsChanged();
    11. void propertyLinkAdded(const PropertyLink* link);
    12. void propertyLinkRemoved(const PropertyLink* link);
    13. void portConnectionAdded(const Port* outport, const Port* inport);
    14. void portConnectionRemoved(const Port* outport, const Port* inport);
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 


    I am planning to change those functions to signals instead and create some slots which will be containing the definition of those respective functions.

    But some times i am sure about the slot functons i have to create . How do i decide that they belong to the corresponding class above or not?



    Regards
    Sajjad

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Quote Originally Posted by stampede View Post
    In addition, it could be quite useful to declare pure virtual slot void Observer::update( Source * changedSource ) = 0; in Observer class, this way all observers will share the same interface for receiving updates.
    You can use QObject::sender() instead.

    Maybe its a little detail, but I'd leave management of Observers to Watched class. In typical observer pattern ("Gang of Four"), Watched object defines methods for adding and removing Observers, so creating connection could be a part of Watched::add(Observer * o) method (and Watched::remove(Observer* o) could simply call this->disconnect(o)).
    If you wrap signals and slots in Observer API then what's the point of using signals and slots in the first place?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    If you wrap signals and slots in Observer API then what's the point of using signals and slots in the first place?
    Because maybe later someone will have to replace signals and slots with other notification mechanism. I know, it's only an exercise, anyway I think its good practice to hide implementation details whenever its possible (this applies to your remark with sender() as well, I know here it will work and OP's quesion was about QObject).

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Quote Originally Posted by stampede View Post
    Because maybe later someone will have to replace signals and slots with other notification mechanism.
    But then why use signals and slots at all? You are implementing a complete observer pattern api only to internally use some other mechanism that already implements the same behaviour. The whole point of using signals and slots is that it is a loosely coupled mechanism. If you wrap it in API that requires some dedicated interfaces then you lose this flexibility and you can use those interfaces directly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Benefit of using signals&slots underneath Observer api could be to send update() notifications to observers in thread-safe manner easily.
    Anyway I think our discussion doesn't help the OP at all

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    I think all the OP wanted was to get rid of additional classes implementing the Observer pattern and use signals and slots directly. All those methods listed in the original post are good candidates for signals.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    My bad, looks like I've misunderstood the original post. Forget what I've written here, you were right from the beginning.

  11. #11
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Have a look at this, I guess you referred to the same example, this library already uses signal & slots.

  12. #12
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Hello Reddy,

    Yes , you are right . They already have it. My question is if i would like to replace their existing observer pattern with signal and slot, what are the procedure do i have to follow?


    Regards
    Sajjad

  13. #13
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Ok, here are some points to start with.

    - The lib you referred uses C++'s polymorphism to make calls to concrete observer class objects from a abstract observer class. You can make use of QtC++'s signals to make these calls.
    - You can replace the virtual functions (be wise, to replace only the virtual functions which are used to realize observer pattern) in the abstract observer class with signals (these signals will still be new observer manager class).
    - All the contrete observer classes which were derived from abstract observer class may no longer need do be so, i.e. they all can be indipendent classes (it's up to your design to have them in class hirarchy, to realize your functunality)
    - In the abstract observer class's abstract implementation replace calls to virtual functions with emitting correspoding signals, as said earlier.
    - In the new classes (which used to be contrete observer classes), implement corresponding slots, and and connect them to the observer manager class's signals (as and when requied to observe)
    - Replace the abstract observer, with a contrete observer manager which manages the observer connections, observable contexts, topics, subjects etc
    - Also, as expected all the new classes should be aware of the observable class, which is genreally used to register (connect a slot) an observation instance with concrete observer manager.
    - When observing to no longer is needed then disconnet the slot (un-registering the observable)

    Note that some of the terms used above may be generic in manner, just to give you a heading.

  14. #14
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Hello Santosh,

    Thanks for the explanation. Now i have the following snippet that needs to be changed to Qt's signal and slot:

    Qt Code:
    1. workspace_->getProcessorNetwork()->addObserver(this);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. connect(processorNetwork, SIGNAL(networkChanged()),this, SLOT());
    To copy to clipboard, switch view to plain text mode 

    In the immediate above code snippet i have changed the virtual functions in the abstract observer class to corresponding and now i am confused to get the SLOT for them. Is there any hint to find the SLOT() for each of the signals. Or i have declare and define them by myself?


    Regards
    Sajjad

  15. #15
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Replace the typical observer pattern to the Qt's signal and slot

    Quote Originally Posted by Santosh Reddy
    - In the new classes (which used to be contrete observer classes), implement corresponding slots, and and connect them to the observer manager class's signals (as and when requied to observe)
    This should answer it...

Similar Threads

  1. Replies: 2
    Last Post: 3rd May 2011, 20:22
  2. Non-typical and Non-linear wizard
    By kornicameister in forum Qt Programming
    Replies: 4
    Last Post: 30th December 2010, 15:26
  3. File Observer in qt
    By succorguy in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2009, 14:21
  4. Observer Design Pattern
    By joseph in forum General Programming
    Replies: 1
    Last Post: 21st January 2008, 12:17
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.