{"id":10447,"date":"2020-10-12T23:03:08","date_gmt":"2020-10-12T14:03:08","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=10447"},"modified":"2020-10-25T09:20:36","modified_gmt":"2020-10-25T00:20:36","slug":"memento-%ed%8c%a8%ed%84%b4","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=10447","title":{"rendered":"[GoF] Memento \ud328\ud134"},"content":{"rendered":"<h4>\ud328\ud134\uba85\uce6d<\/h4>\n<p>Memento<\/p>\n<h4>\ud544\uc694\ud55c \uc0c1\ud669<\/h4>\n<p>\uc2dc\uac04\uc5d0 \ub530\ub978 \uc21c\ucc28\uc801\uc778 \uc5f0\uc0b0\uc744 \uc218\ud589\ud558\uba74\uc11c, \uc6d0\ud558\ub294 \uc2dc\uc810\uc758 \uc0c1\ud0dc\ub85c \ub418\ub3cc\uc544\uac08 \ud544\uc694\uac00 \uc788\uc744\ub54c \uc0ac\uc6a9\ud558\ub294 \ud328\ud134\uc774\ub2e4.<\/p>\n<h4>\uc608\uc81c \ucf54\ub4dc <\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2020\/10\/memento.png\" alt=\"\" width=\"800\"  class=\"aligncenter size-full wp-image-10448\" \/><\/p>\n<p>Walker \ud074\ub798\uc2a4\uc758 \uac1d\uccb4\ub294 \uc704, \uc624\ub978\ucabd, \uc544\ub798, \uc67c\ucabd\uc73c\ub85c 1 \ub2e8\uc704\uc529 \uc774\ub3d9\ud558\uc5ec \uc810\uc810 Target \ud074\ub798\uc2a4\uc758 \uac1d\uccb4\uc5d0 \uc800\uc7a5\ub41c x, y \uc88c\ud45c\ub85c \uac00\ub3c4\ub85d \uac00\uc7a5 \ucd5c\uc801\uc758 Walker \uac1d\uccb4\uc758 \uc0c1\ud0dc\ub97c Memento \ud074\ub798\uc2a4\ub97c \uc774\uc6a9\ud574 \uc800\uc7a5\ud558\uace0 \ub2e4\uc2dc \ubcf5\uc6d0\ud55c\ub2e4. Memento \ud074\ub798\uc2a4\ub294 Walker\uc758 inner class\uc778\ub370, \uc774\uc720\ub294 \uc624\uc9c1 Walker\uc5d0\ub9cc \uc815\ubcf4\ub97c \uc804\ub2ec\ud558\uae30 \uc704\ud568\uc774\ub2e4. \ub3d9\uc2dc\uc5d0 Memento\uc758 \uc0dd\uc131\uc790\ub294 private\uc778\ub370 \uc774\ub294 \uc624\uc9c1 Walker\ub9cc\uc774 Memento \uac1d\uccb4\ub97c \uc0dd\uc131\ud560 \uc218 \uc788\ub3c4\ub85d \ud558\uae30 \uc704\ud568\uc774\ub2e4. \uba3c\uc800 Target \ud074\ub798\uc2a4\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Target {\r\n\tprivate int x;\r\n\tprivate int y;\r\n\t\r\n\tpublic Target(int x, int y) {\r\n\t\tthis.x = x;\r\n\t\tthis.y = y;\r\n\t}\r\n\t\r\n\tpublic double getDictance(int x0, int y0) {\r\n\t\treturn Math.sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0));\r\n\t}\r\n}\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 Action \uc5f4\uac70\uc790\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic enum Action {\r\n\tUP, RIGHT, DOWN, LEFT;\r\n\t\r\n\tpublic static Action fromIndex(int i) {\r\n\t\tif(i == 0) return UP;\r\n\t\telse if(i == 1) return RIGHT;\r\n\t\telse if(i == 2) return DOWN;\r\n\t\telse return LEFT;\t\r\n\t}\r\n}\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 Walker \ud074\ub798\uc2a4\uc640 \uadf8 \ub0b4\ubd80 \ud074\ub798\uc2a4\uc778 Memento \ud074\ub798\uc2a4\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\nimport java.util.ArrayList;\r\n\r\npublic class Walker {\r\n\tprivate int x;\r\n\tprivate int y;\r\n\tprivate ArrayList&lt;Action> actions = new ArrayList&lt;Action>();\r\n\t\t\r\n\tpublic Walker(int x, int y) {\r\n\t\tthis.x = x;\r\n\t\tthis.y = y;\r\n\t}\r\n\t\r\n\tpublic double walk(Action action, Target target) {\r\n\t\tactions.add(action);\r\n\t\t\r\n\t\tif(action == Action.UP) {\r\n\t\t\ty += 1;\r\n\t\t} else if(action == Action.RIGHT) {\r\n\t\t\tx += 1;\r\n\t\t} else if(action == Action.DOWN) {\r\n\t\t\ty -= 1;\r\n\t\t} else if(action == Action.LEFT) {\r\n\t\t\tx -= 1;\r\n\t\t}\r\n\t\t\r\n\t\treturn target.getDictance(x, y);\r\n\t}\r\n\t\r\n\tpublic class Memento {\r\n\t\tprivate int x;\r\n\t\tprivate int y;\r\n\t\tprivate ArrayList&lt;Action&lt; actions;\r\n\t\t\r\n\t\tprivate Memento() {}\r\n\t}\r\n\t\r\n\tpublic Memento createMemento() {\r\n\t\tMemento memento = new Memento();\r\n\t\t\r\n\t\tmemento.x = this.x;\r\n\t\tmemento.y = this.y;\r\n\t\tmemento.actions = (ArrayList&lt;Action>)this.actions.clone();\r\n\t\t\r\n\t\treturn memento;\r\n\t}\r\n\t\r\n\tpublic void restoreMemento(Memento memento) {\r\n\t\tthis.x = memento.x;\r\n\t\tthis.y = memento.y;\r\n\t\tthis.actions = (ArrayList&lt;Action>)memento.actions.clone();\r\n\t}\r\n\t\r\n\tpublic String toString() {\r\n\t\treturn actions.toString();\r\n\t}\r\n}\r\n<\/pre>\n<p>\ub05d\uc73c\ub85c \uc9c0\uae08\uae4c\uc9c0\uc758 \ud074\ub798\uc2a4\ub4e4\uc744 \uc0ac\uc6a9\ud558\ub294 \ucf54\ub4dc\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\nimport java.util.Random;\r\n\r\nimport tstThread.Walker.Memento;\r\n\r\npublic class Main {\r\n\tpublic static void main(String[] args) {\r\n\t\tTarget target = new Target(10, 10);\r\n\t\tWalker walker = new Walker(0, 0);\r\n\t\t\r\n\t\tRandom random = new Random();\r\n\t\t\r\n\t\tdouble minDistance = Double.MAX_VALUE;\r\n\t\tMemento memento = null;\r\n\t\twhile(true) {\r\n\t\t\tAction nextAction = Action.fromIndex(random.nextInt(4));\r\n\t\t\tdouble distance = walker.walk(nextAction, target);\r\n\t\t\t\r\n\t\t\t\r\n\t\t\tSystem.out.println(nextAction + \" \" + distance);\r\n\t\t\t\r\n\t\t\tif(distance == 0.0) {\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(minDistance > distance) {\r\n\t\t\t\tminDistance = distance;\r\n\t\t\t\tmemento = walker.createMemento();\r\n\t\t\t} else {\r\n\t\t\t\tif(memento != null) {\r\n\t\t\t\t\twalker.restoreMemento(memento);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(\"Walker's path: \" + walker);\r\n\t}\r\n}\r\n<\/pre>\n<p>\uc2e4\ud589 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class='code'>\r\nRIGHT 13.45362404707371\r\nLEFT 14.142135623730951\r\nRIGHT 12.806248474865697\r\nDOWN 13.601470508735444\r\nUP 12.041594578792296\r\nUP 11.313708498984761\r\nDOWN 12.041594578792296\r\nDOWN 12.041594578792296\r\nDOWN 12.041594578792296\r\nRIGHT 10.63014581273465\r\nRIGHT 10.0\r\nUP 9.219544457292887\r\nLEFT 9.899494936611665\r\nRIGHT 8.602325267042627\r\nLEFT 9.219544457292887\r\nLEFT 9.219544457292887\r\nLEFT 9.219544457292887\r\nLEFT 9.219544457292887\r\nUP 7.810249675906654\r\nLEFT 8.48528137423857\r\nRIGHT 7.211102550927978\r\nDOWN 8.06225774829855\r\nUP 6.4031242374328485\r\nDOWN 7.211102550927978\r\nRIGHT 5.830951894845301\r\nDOWN 6.708203932499369\r\nDOWN 6.708203932499369\r\nUP 5.0\r\nDOWN 5.830951894845301\r\nDOWN 5.830951894845301\r\nRIGHT 4.47213595499958\r\nDOWN 5.385164807134504\r\nRIGHT 4.123105625617661\r\nDOWN 5.0990195135927845\r\nRIGHT 4.0\r\nLEFT 4.123105625617661\r\nDOWN 5.0\r\nDOWN 5.0\r\nRIGHT 4.123105625617661\r\nUP 3.0\r\nUP 2.0\r\nLEFT 2.23606797749979\r\nUP 1.0\r\nLEFT 1.4142135623730951\r\nLEFT 1.4142135623730951\r\nDOWN 2.0\r\nUP 0.0\r\nWalker's path: [RIGHT, RIGHT, UP, UP, RIGHT, RIGHT, UP, RIGHT, UP, RIGHT, UP, RIGHT, UP, RIGHT, RIGHT, RIGHT, UP, UP, UP, UP]\r\n<\/pre>\n<p>Walker \uac1d\uccb4\uac00 \uc5b4\ub5a4 \ud589\uc704\ub97c \ucde8\ud588\uc744\ub54c \uc774\uc804\uc758 \ud589\uc704\uc5d0 \ub300\ud55c \ubaa9\uc801\uc9c0\uae4c\uc9c0\uc758 \uac70\ub9ac\ubcf4\ub2e4 \ub354 \uba40\uc5b4\uc9c0\uba74, \uc774\uc804\uc758 \uc0c1\ud0dc\ub85c \ub3cc\uc544\uac00\uae30 \uc704\ud574 Memento \uac1d\uccb4\ub97c \uc0ac\uc6a9\ud558\uace0 \uc788\ub2e4.<\/p>\n<div style='background:#efefef;border-radius:8px;padding:12px 24px'>\uc774 \uae00\uc740 \uc18c\ud504\ud2b8\uc6e8\uc5b4 \uc124\uacc4\uc758 \uae30\ubc18\uc774 \ub418\ub294 GoF\uc758 \ub514\uc790\uc778\ud328\ud134\uc5d0 \ub300\ud55c \uac15\uc758\uc790\ub8cc\uc785\ub2c8\ub2e4. \uc644\uc804\ud55c \uc2e4\uc2b5\uc744 \uc704\ud574 \uc774 \uae00\uc5d0\uc11c \uc18c\uac1c\ud558\ub294 \ud074\ub798\uc2a4 \ub2e4\uc774\uc5b4\uadf8\ub7a8\uacfc \uc608\uc81c \ucf54\ub4dc\ub294 \uc644\uc804\ud558\uac8c \uc2e4\ud589\ub418\ub3c4\ub85d \uc81c\uacf5\ub418\uc9c0\ub9cc, \uc0c1\ub300\uc801\uc73c\ub85c \uc608\uc81c \ucf54\ub4dc\uc640 \uad00\ub828\ub41c \uc124\uba85\uc774 \ud568\ucd95\uc801\uc73c\ub85c \uc81c\uacf5\ub418\uace0 \uc788\uc2b5\ub2c8\ub2e4. \uc774 \uae00\uc5d0 \ub300\ud574 \uad81\uae08\ud55c \uc810\uc774 \uc788\uc73c\uba74 \ub313\uae00\uc744 \ud1b5\ud574 \ub0a8\uaca8\uc8fc\uc2dc\uae30 \ubc14\ub78d\ub2c8\ub2e4.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\ud328\ud134\uba85\uce6d Memento \ud544\uc694\ud55c \uc0c1\ud669 \uc2dc\uac04\uc5d0 \ub530\ub978 \uc21c\ucc28\uc801\uc778 \uc5f0\uc0b0\uc744 \uc218\ud589\ud558\uba74\uc11c, \uc6d0\ud558\ub294 \uc2dc\uc810\uc758 \uc0c1\ud0dc\ub85c \ub418\ub3cc\uc544\uac08 \ud544\uc694\uac00 \uc788\uc744\ub54c \uc0ac\uc6a9\ud558\ub294 \ud328\ud134\uc774\ub2e4. \uc608\uc81c \ucf54\ub4dc Walker \ud074\ub798\uc2a4\uc758 \uac1d\uccb4\ub294 \uc704, \uc624\ub978\ucabd, \uc544\ub798, \uc67c\ucabd\uc73c\ub85c 1 \ub2e8\uc704\uc529 \uc774\ub3d9\ud558\uc5ec \uc810\uc810 Target \ud074\ub798\uc2a4\uc758 \uac1d\uccb4\uc5d0 \uc800\uc7a5\ub41c x, y \uc88c\ud45c\ub85c \uac00\ub3c4\ub85d \uac00\uc7a5 \ucd5c\uc801\uc758 Walker \uac1d\uccb4\uc758 \uc0c1\ud0dc\ub97c Memento \ud074\ub798\uc2a4\ub97c \uc774\uc6a9\ud574 \uc800\uc7a5\ud558\uace0 \ub2e4\uc2dc \ubcf5\uc6d0\ud55c\ub2e4. Memento \ud074\ub798\uc2a4\ub294 Walker\uc758 inner class\uc778\ub370, \uc774\uc720\ub294 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=10447\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;[GoF] Memento \ud328\ud134&#8221;<\/span><\/a><\/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":[],"class_list":["post-10447","post","type-post","status-publish","format-standard","hentry","category-design"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10447"}],"version-history":[{"count":6,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10447\/revisions"}],"predecessor-version":[{"id":10562,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10447\/revisions\/10562"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}