{"id":4193,"date":"2019-12-18T09:32:17","date_gmt":"2019-12-18T09:32:17","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=4193"},"modified":"2024-03-07T11:31:11","modified_gmt":"2024-03-07T11:31:11","slug":"state-design-pattern","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=4193","title":{"rendered":"State (Design Pattern)"},"content":{"rendered":"<p>This behavioral pattern is used to let objects behave differently based on their current state.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nclass GirlfriendState;\r\n\r\nclass Girlfriend {\r\npublic:\r\n    Girlfriend();\r\n    ~Girlfriend();\r\n    void givePresent();\r\n    void forgetBirthday();\r\nprivate:\r\n    friend class GirlfriendState;\r\n    GirlfriendState* m_State;\r\n};\r\n\r\nclass GirlfriendState {\r\npublic:\r\n    virtual void givePresent(GirlfriendState* state) {}\r\n    virtual void forgetBirthday(GirlfriendState* state) {}\r\n};\r\nclass HappyState : public GirlfriendState {\r\npublic:\r\n    void forgetBirthday(GirlfriendState *state);\r\n};\r\nclass AngryState : public GirlfriendState {\r\npublic:\r\n    void givePresent(GirlfriendState *state);\r\n};\r\n\r\nGirlfriend::Girlfriend() { m_State = new HappyState(); }\r\nGirlfriend::~Girlfriend() { delete m_State; }\r\nvoid Girlfriend::givePresent() {\r\n    m_State-&gt;givePresent(this-&gt;m_State);\r\n}\r\nvoid Girlfriend::forgetBirthday() {\r\n    m_State-&gt;forgetBirthday(this-&gt;m_State);\r\n}\r\n\r\nvoid HappyState::forgetBirthday(GirlfriendState* state) {\r\n    delete state;\r\n    state = new AngryState();\r\n}\r\nvoid AngryState::givePresent(GirlfriendState* state) {\r\n    delete state;\r\n    state = new HappyState();\r\n}\r\n\r\nint main() {\r\n    Girlfriend gf;\r\n    gf.forgetBirthday();\r\n    gf.givePresent();\r\n    \/\/ ...\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This behavioral pattern is used to let objects behave differently based on their current state.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/4193"}],"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=4193"}],"version-history":[{"count":3,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4193\/revisions"}],"predecessor-version":[{"id":16995,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4193\/revisions\/16995"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4193"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}