3 ms·
What are signals/slots the best paradigm for?
by EGreg 11y ago
What are signals/slots the best paradigm for?
- lordnacho 11y agoObserver pattern. Say you have some event that occurs, and some objects that want to know about it. You make a signal and connect some observers to it. When you fire the signal the observers receive some parameters and do their calculations.
- ajuc 11y agoWhy it's interesting alternative: regular observer pattern make observed code depend (at compile time) on the observing code. Signals/slots remove this dependency, and allow for external configuration of these. It's a dependency injection mechanism when you think about it.
- truncate 11y agoQt Framework uses them extensively. You might find pretty good examples there. Its been a long while, so I can't specifically give examples.
- hellofunk 11y agoThey are a useful mechanism for linking different parts of a program without having to couple objects together. OOP can lead to a lot of inflexibility, but historically has been the most obvious mechanism in C++ for one object to learn about what another object is doing: pass a pointer to itself, etc. But this often requires inheritance, polymorphism, virtual interfaces, a number of idioms that can complicate this basic requirement. Signals and slots allow the objects to not know anything about each other, not store pointers to each other, be more or less isolated, except for this one little connection between them that is otherwise anonymous to both.