{"id":10436,"date":"2020-10-11T15:12:50","date_gmt":"2020-10-11T06:12:50","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=10436"},"modified":"2020-10-25T09:20:47","modified_gmt":"2020-10-25T00:20:47","slug":"factory-method-%ed%8c%a8%ed%84%b4","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=10436","title":{"rendered":"[GoF] Factory Method \ud328\ud134"},"content":{"rendered":"<h4>\ud328\ud134\uba85\uce6d<\/h4>\n<p>Factory Method<\/p>\n<h4>\ud544\uc694\ud55c \uc0c1\ud669<\/h4>\n<p>\uac1d\uccb4\uc758 \uc0dd\uc131\uc5d0 \ub300\ud574 Template \ud328\ud134\uc744 \ud65c\uc6a9\ud55c \uacbd\uc6b0\uc774\ub2e4. \uac1d\uccb4\ub97c \uc548\uc804\ud558\uace0 \uc644\ubcbd\ud558\uac8c \uc0dd\uc131\ud558\uae30 \uc704\ud55c \ubc29\ubc95\uc744 \uc0c1\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \uc815\ud574 \ub193\uace0, \uadf8 \ubc29\ubc95\uc5d0 \ub300\ud55c \uad6c\uccb4\uc801\uc778 \uad6c\ud604\uc740 \ud558\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \uc815\uc758\ud55c\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\/factoryMethod.png\" alt=\"\" width=\"800\" class=\"aligncenter size-full wp-image-10437\" \/><\/p>\n<p>\ub2e4\uc591\ud55c \ud615\ud0dc\uc758 \ub3c4\ud615\uc744 \uc0dd\uc131\uc744 \uc704\ud574 \ud544\uc694\ud55c \uc808\ucc28\ub97c Factory \ud074\ub798\uc2a4\uc5d0 \uc815\uc758\ud574 \ub193\uace0, \uac01 \uc808\ucc28\uc5d0 \ub300\ud55c \uad6c\uccb4\uc801\uc778 \uad6c\ud604\uc740 SyncFactory \ud074\ub798\uc2a4\uc5d0\uc11c \uc815\uc758\ud55c\ub2e4. \uba3c\uc800 Factory \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic abstract class Factory {\r\n\tpublic Shape create(Shape newShape) {\r\n\t\tif(register(newShape)) {\r\n\t\t\tif(uploadToServer(newShape)) {\r\n\t\t\t\treturn newShape;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\treturn null;\r\n\t}\r\n\t\r\n\tprotected abstract boolean register(Shape shape);\r\n\tprotected abstract boolean uploadToServer(Shape shape);\r\n}\r\n<\/pre>\n<p>\uba3c\uc800 \uc0dd\uc131\ub41c \ucd08\ubc8c \uac1d\uccb4\ub97c \uc778\uc790\ub85c \ubc1b\uc544 \ub4f1\ub85d\ud558\uace0, \uc11c\ubc84\ub85c \ud574\ub2f9 \ub3c4\ud615\uc744 \uc804\uc1a1\uae4c\uc9c0 \uc131\uacf5\ud558\uba74 \ucc98\ub9ac\ub41c \uac1d\uccb4\ub97c \ubc18\ud658\ud558\uc9c0\ub9cc \uc808\ucc28\uc911 \ud558\ub098\ub77c\ub3c4 \uc2e4\ud328\ud558\uba74 null\uc744 \ubc18\ud658\ud55c\ub2e4. \ucd08\ubc8c \uac1d\uccb4\uc758 \uc0dd\uc131\uc740 \uc778\uc790\ub85c \ubc1b\uc558\uc73c\ub098 create \ub9e4\uc11c\ub4dc \ub0b4\ubd80\uc5d0\uc11c \uc218\ud589\ud574\ub3c4 \ub41c\ub2e4. \uc644\uc804\ud55c \uac1d\uccb4 \uc0dd\uc131\uc744 \uc704\ud55c \uc808\ucc28\ub97c \uad6c\uccb4\uc801\uc73c\ub85c \uad6c\ud604\ud55c SyncFactory \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\nimport java.util.HashMap;\r\n\r\npublic class SyncFactory extends Factory {\r\n\tprivate HashMap&lt;String, Shape> shapes = new HashMap&lt;String, Shape>();\r\n\r\n\t@Override\r\n\tprotected boolean register(Shape shape) {\r\n\t\tString name = shape.getName();\r\n\t\t\r\n\t\tif(!shapes.containsKey(name)) {\r\n\t\t\tshapes.put(name, shape);\r\n\t\t\tSystem.out.println(\"registering shape(\" + shape + \") is successed.\");\r\n\t\t\treturn true;\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(\"registering shape(\" + shape + \") is failed.\");\r\n\t\treturn false;\r\n\t}\r\n\r\n\t@Override\r\n\tprotected boolean uploadToServer(Shape shape) {\r\n\t\ttry {\r\n\t\t\tThread.sleep(1000);\r\n\t\t\tSystem.out.println(\"uploading shape(\" + shape + \") is successed.\");\r\n\t\t\treturn true;\r\n\t\t} catch(Exception e) {\r\n\t\t\tSystem.out.println(\"uploading shape(\" + shape + \") is failed.\");\r\n\t\t\treturn false;\r\n\t\t}\t\t\r\n\t}\r\n}\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 \uc0dd\uc131\ud560 \ub3c4\ud615\uc5d0 \ub300\ud55c Shape \ud074\ub798\uc2a4\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic abstract class Shape {\r\n\tprotected String name;\r\n\t\r\n\tpublic Shape(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n\t\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\t\r\n\tpublic String toString() {\r\n\t\treturn name;\r\n\t}\r\n\t\r\n\tpublic abstract void draw();\r\n}\r\n<\/pre>\n<p>\uc774 Shape \ud074\ub798\uc2a4\ub97c \uad6c\ud604\ud55c \uad6c\uccb4\uc801\uc778 \ud30c\uc0dd \ud074\ub798\uc2a4 \uc911 Circle \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Circle extends Shape {\r\n\tprivate int radius;\r\n\t\r\n\tpublic Circle(String name, int radius) {\r\n\t\tsuper(name);\r\n\t\tthis.radius = radius;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void draw() {\r\n\t\tSystem.out.println(\"Circle(\" + name + \") draw : radis = \" + radius);\r\n\t}\r\n}\r\n<\/pre>\n<p>Box \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Box extends Shape {\r\n\tprivate int width;\r\n\tprivate int height;\r\n\t\r\n\tpublic Box(String name, int width, int height) {\r\n\t\tsuper(name);\r\n\t\tthis.width = width;\r\n\t\tthis.height = height;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void draw() {\r\n\t\tSystem.out.println(\"Circle(\" + name + \") draw : width = \" + width + \", height = \" + height);\r\n\t}\r\n}\r\n<\/pre>\n<p>\uc9c0\uae08\uae4c\uc9c0 \ub9cc\ub4e0 \ud074\ub798\uc2a4\ub97c \uc0ac\uc6a9\ud558\ub294 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstThread;\r\n\r\npublic class Main {\r\n\tpublic static void main(String[] args) {\r\n\t\tFactory factory = new SyncFactory();\r\n\t\tShape shape;\r\n\t\t\r\n\t\tshape = factory.create(new Circle(\"SHAPE1\", 10));\r\n\t\tif(shape != null) shape.draw();\r\n\t\tSystem.out.println();\r\n\t\t\r\n\t\tshape = factory.create(new Box(\"SHAPE2\", 10, 20));\r\n\t\tif(shape != null) shape.draw();\r\n\t\tSystem.out.println();\r\n\t\t\r\n\t\tshape = factory.create(new Box(\"SHAPE1\", 5, 10));\r\n\t\tif(shape != null) shape.draw();\r\n\t\tSystem.out.println();\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\nregistering shape(SHAPE1) is successed.\r\nuploading shape(SHAPE1) is successed.\r\nCircle(SHAPE1) draw : radis = 10\r\n\r\nregistering shape(SHAPE2) is successed.\r\nuploading shape(SHAPE2) is successed.\r\nCircle(SHAPE2) draw : width = 10, height = 20\r\n\r\nregistering shape(SHAPE1) is failed.\r\n<\/pre>\n<p>\ucd1d 3\uac1c\uc758 \ub3c4\ud615\uc744 \uc0dd\uc131\ud588\ub294\ub370, \ub9c8\uc9c0\ub9c9 \ub3c4\ud615\uc758 \uacbd\uc6b0 \uc774\ubbf8 \ub3d9\uc77c\ud55c \uc774\ub984(name)\uc744 \uac16\uace0 \ub3c4\ud615\uc774 \uba3c\uc800 \uc0dd\uc131\ub418\uc5b4\uc838 \uc788\uc5b4 \uc2e4\ud328\ud558\uac8c \ub41c\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 Factory Method \ud544\uc694\ud55c \uc0c1\ud669 \uac1d\uccb4\uc758 \uc0dd\uc131\uc5d0 \ub300\ud574 Template \ud328\ud134\uc744 \ud65c\uc6a9\ud55c \uacbd\uc6b0\uc774\ub2e4. \uac1d\uccb4\ub97c \uc548\uc804\ud558\uace0 \uc644\ubcbd\ud558\uac8c \uc0dd\uc131\ud558\uae30 \uc704\ud55c \ubc29\ubc95\uc744 \uc0c1\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \uc815\ud574 \ub193\uace0, \uadf8 \ubc29\ubc95\uc5d0 \ub300\ud55c \uad6c\uccb4\uc801\uc778 \uad6c\ud604\uc740 \ud558\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \uc815\uc758\ud55c\ub2e4. \uc608\uc81c \ucf54\ub4dc \ub2e4\uc591\ud55c \ud615\ud0dc\uc758 \ub3c4\ud615\uc744 \uc0dd\uc131\uc744 \uc704\ud574 \ud544\uc694\ud55c \uc808\ucc28\ub97c Factory \ud074\ub798\uc2a4\uc5d0 \uc815\uc758\ud574 \ub193\uace0, \uac01 \uc808\ucc28\uc5d0 \ub300\ud55c \uad6c\uccb4\uc801\uc778 \uad6c\ud604\uc740 SyncFactory \ud074\ub798\uc2a4\uc5d0\uc11c \uc815\uc758\ud55c\ub2e4. \uba3c\uc800 Factory \ud074\ub798\uc2a4\ub294 \ub2e4\uc74c\uacfc &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=10436\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;[GoF] Factory Method \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-10436","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\/10436","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=10436"}],"version-history":[{"count":4,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10436\/revisions"}],"predecessor-version":[{"id":10564,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10436\/revisions\/10564"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10436"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}