Results 1 to 15 of 15

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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)).

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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

    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).

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  5. #5
    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  7. #7
    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.

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.