c++ - How to use signals and slots for observer pattern? -
i wrote simple observer pattern, observer has "void notify(std::string)" function , observable object calls on every registered observer , uses tokenized string transfer data. simple, easy , works, need go step forward.
i need implement signal , slots (for illustration using boost::signals2). don't know how slot , signals should , how should placed. have no thought how allow registering whatever function want , not void (string).
i couldn't find resource uses signals , slots pattern. yet says signals , slots amazing observer pattern. guide me how should signals , slots used observer pattern?
my current implementation without signals follows:
class observable { public: void register(observer *); void unregister(observer *); protected: void notifyobservers() { every registered observer observer.notify(std::string tokenized_string); } } class observer { public: void notify(std::string) = 0; }
i need alter pattern utilize signals , slots have no thought how should useful , well-designed , flexible.
how observer pattern work ?
observable objects "watched" observer objects when observable modified, notifies of observers alter has been made, illustration calling "update" or "notify" function (or whatever)so, in terms of signals , slots. basics connect signals slots, called each time signal they're connected emitted.
you find easy utilize in observer pattern : create signal in observable connected slot of each observer used update it.
in case of alter in observable. instead of looping through list of observers, , calling update
method 1 after another, emit signal. corresponding slots magically called.
to go further, illustration have slot in observable, connected signal observer emit tell observable has noticed in case of changes...
can't working illustration code since never used boost::signals2
, should take @ how utilize boost::signals implement observer pattern? , observer design pattern in c++ going :)
c++ design-patterns signals observer-pattern signals-slots
No comments:
Post a Comment