{"id":560,"date":"2017-06-07T15:48:01","date_gmt":"2017-06-07T15:48:01","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=560"},"modified":"2024-02-16T10:05:20","modified_gmt":"2024-02-16T10:05:20","slug":"observer-pattern-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=560","title":{"rendered":"Observer (Design pattern)"},"content":{"rendered":"<p>In this behavioral design pattern all observer getting notified of a change from a subject they are registered at.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;string&gt;\r\n#include &lt;set&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\n\/\/ --- Observer ----------\r\nclass Observer\r\n{\r\npublic:\r\n    virtual void onInput(string) = 0;\r\n};\r\n\r\n\/\/ --- Subject ----------\r\nclass Subject\r\n{\r\npublic:\r\n    virtual void attach(Observer*) = 0;\r\n    virtual void detach(Observer*) = 0;\r\n    virtual void input(string) = 0;\r\n};\r\n\r\n\/\/ --- Implementation ---\r\nclass View : Subject\r\n{\r\nprivate:\r\n    set&lt;Observer*&gt; obs;\r\npublic:\r\n    void attach(Observer *ob){obs.insert(ob);}\r\n    void detach(Observer *ob){obs.erase(ob);}\r\n    void input(string input)\r\n    {\r\n        for(auto ob : obs){\r\n            ob-&gt;onInput(input);\r\n        }\r\n    }\r\n};\r\n\r\nclass Parser : Observer\r\n{\r\npublic:\r\n    void onInput(string input)\r\n    {\r\n        parse(input);\r\n    }\r\nprivate:\r\n    void parse(string input)\r\n    {\r\n        cout &lt;&lt; &quot;Parse: &quot; + input &lt;&lt; endl;\r\n    }\r\n};\r\n\r\n\r\nint main()\r\n{\r\n    View view;\r\n    Parser parser;\r\n    view.attach((Observer*)&amp;parser);\r\n    \r\n    \/\/ --- Emulate Input ---\r\n    view.input(&quot;(3+2)*4&quot;);\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this behavioral design pattern all observer getting notified of a change from a subject they are registered at.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[30],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/560"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=560"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions"}],"predecessor-version":[{"id":16700,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/560\/revisions\/16700"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}