{"id":328,"date":"2013-06-14T11:56:14","date_gmt":"2013-06-14T11:56:14","guid":{"rendered":"http:\/\/yoonhada.com\/wp\/?p=328"},"modified":"2013-06-14T11:56:14","modified_gmt":"2013-06-14T11:56:14","slug":"state-pattorn","status":"publish","type":"post","link":"http:\/\/yoonhada.com\/?p=328","title":{"rendered":"state pattern"},"content":{"rendered":"<p>\t\t\t\t<a href=\"http:\/\/yoonhada.com\/wp\/wp-content\/uploads\/2013\/06\/\uc81c\ubaa9-\uc5c6\uc74c1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-329\" alt=\"\uc81c\ubaa9 \uc5c6\uc74c\" src=\"http:\/\/yoonhada.com\/wp\/wp-content\/uploads\/2013\/06\/\uc81c\ubaa9-\uc5c6\uc74c1.png\" width=\"896\" height=\"354\" \/><\/a><\/p>\n<p>[cpp]<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>using namespace std;<\/p>\n<p>class GameLevel<br \/>\n{<br \/>\npublic:<br \/>\n static GameLevel* CreateInstance() { return 0; }<br \/>\n virtual void SimpleAttack() = 0;<br \/>\n virtual void TurnAttack() = 0;<br \/>\n virtual void FlyingAttack() = 0;<br \/>\n};<\/p>\n<p>class GameLevel0 : public GameLevel<br \/>\n{<br \/>\npublic:<br \/>\n static GameLevel* CreateInstance()<br \/>\n {<br \/>\n if( pInstance_ == NULL ) pInstance_ = new GameLevel0;<br \/>\n return pInstance_;<br \/>\n }<\/p>\n<p>virtual void SimpleAttack() { cout &lt;&lt; &quot;SimpleAttack()&quot; &lt;&lt; endl; }<br \/>\n virtual void TurnAttack() { cout &lt;&lt; &quot;Not Allowed&quot; &lt;&lt; endl; }<br \/>\n virtual void FlyingAttack() { cout &lt;&lt; &quot;Not Allowed&quot; &lt;&lt; endl; }<\/p>\n<p>protected:<br \/>\n static GameLevel0 * pInstance_;<br \/>\n};<br \/>\nGameLevel0* GameLevel0::pInstance_ = 0;<\/p>\n<p>class GameLevel1 : public GameLevel<br \/>\n{<br \/>\npublic:<br \/>\n static GameLevel* CreateInstance()<br \/>\n {<br \/>\n if( pInstance_ == NULL ) pInstance_ = new GameLevel1;<br \/>\n return pInstance_;<br \/>\n }<\/p>\n<p>virtual void SimpleAttack() { cout &lt;&lt; &quot;SimpleAttack()&quot; &lt;&lt; endl; }<br \/>\n virtual void TurnAttack() { cout &lt;&lt; &quot;TurnAttack()&quot; &lt;&lt; endl; }<br \/>\n virtual void FlyingAttack() { cout &lt;&lt; &quot;Not Allowed&quot; &lt;&lt; endl; }<\/p>\n<p>protected:<br \/>\n static GameLevel1 * pInstance_;<br \/>\n};<br \/>\nGameLevel1 * GameLevel1::pInstance_ = NULL;<\/p>\n<p>class GameLevel2 : public GameLevel<br \/>\n{<br \/>\npublic:<br \/>\n static GameLevel* CreateInstance()<br \/>\n {<br \/>\n if( pInstance_ == NULL ) pInstance_ = new GameLevel2;<br \/>\n return pInstance_;<br \/>\n }<\/p>\n<p>virtual void SimpleAttack() { cout &lt;&lt; &quot;SimpleAttack()&quot; &lt;&lt; endl; }<br \/>\n virtual void TurnAttack() { cout &lt;&lt; &quot;TurnAttack()&quot; &lt;&lt; endl; }<br \/>\n virtual void FlyingAttack() { cout &lt;&lt; &quot;FlyingAttack()&quot; &lt;&lt; endl; }<\/p>\n<p>protected:<br \/>\n static GameLevel2 * pInstance_;<br \/>\n};<br \/>\nGameLevel2 * GameLevel2::pInstance_ = NULL;<\/p>\n<p>class GamePlayer<br \/>\n{<br \/>\npublic:<br \/>\n GamePlayer() { pGameLevel_ = GameLevel0::CreateInstance(); }<\/p>\n<p>void Updatelevel( GameLevel * pLevel) { pGameLevel_ = pLevel; }<\/p>\n<p>virtual void SimpleAttack() { pGameLevel_-&gt;SimpleAttack(); }<br \/>\n virtual void TurnAttack() { pGameLevel_-&gt;TurnAttack(); }<br \/>\n virtual void FlyingAttack() { pGameLevel_-&gt;FlyingAttack(); }<\/p>\n<p>private:<br \/>\n GameLevel * pGameLevel_;<br \/>\n};<\/p>\n<p>int main()<br \/>\n{<br \/>\n GamePlayer user1;<\/p>\n<p>user1.SimpleAttack();<br \/>\n user1.TurnAttack();<br \/>\n user1.FlyingAttack();<\/p>\n<p>cout &lt;&lt; &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot; &lt;&lt; endl;<\/p>\n<p>user1.Updatelevel( GameLevel1::CreateInstance() );<br \/>\n user1.SimpleAttack();<br \/>\n user1.TurnAttack();<br \/>\n user1.FlyingAttack();<\/p>\n<p>cout &lt;&lt; &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot; &lt;&lt; endl;<\/p>\n<p>user1.Updatelevel( GameLevel2::CreateInstance() );<br \/>\n user1.SimpleAttack();<br \/>\n user1.TurnAttack();<br \/>\n user1.FlyingAttack();<\/p>\n<p>cout &lt;&lt; &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot; &lt;&lt; endl;<br \/>\n return 0;<br \/>\n}<\/p>\n<p>[\/cpp]<\/p>\n<p>\uad6c\ud604\uad00\ub828 \uc0ac\ud56d<br \/>\n&#8211; State\ud328\ud134\uc740 \uc0c1\ud0dc\ubcc0\ud654\uc5d0 \ub530\ub77c \ud589\uc704 \uc218\ud589 \ubcc0\uacbd\uc774 \uc790\ub3d9\uc73c\ub85c \uc774\ub8e8\uc5b4 \uc9c0\uac8c \ub9cc\ub4e4\uae30 \uc704\ud55c \uac83. \uc0c1\ud0dc\ubcc0\ud658\uc744 \uc27d\uac8c \ud558\uae30 \uc704\ud574 \uc124\uacc4\ub41c\uac83\uc774 \uc544\ub2d8.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[cpp] #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; class GameLevel { public: static GameLevel* CreateInstance() { return 0; } virtual void SimpleAttack() = 0; virtual void TurnAttack() = 0; virtual void FlyingAttack() = 0; }; class GameLevel0 : public GameLevel { public: static GameLevel* CreateInstance() { if( pInstance_ == NULL ) pInstance_ = new GameLevel0; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[33],"class_list":["post-328","post","type-post","status-publish","format-standard","hentry","category-programming","tag-pattern"],"_links":{"self":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts\/328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328"}],"version-history":[{"count":0,"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts\/328\/revisions"}],"wp:attachment":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}