{"id":4421,"date":"2023-04-05T13:34:15","date_gmt":"2023-04-05T04:34:15","guid":{"rendered":"https:\/\/m9js.shop\/blog\/?p=4421"},"modified":"2023-04-29T18:52:42","modified_gmt":"2023-04-29T09:52:42","slug":"template-method-pattern-increase-code-reusability-by-abstracting-and-refining-common-algorithms","status":"publish","type":"post","link":"https:\/\/m9js.shop\/blog\/development\/template-method-pattern-increase-code-reusability-by-abstracting-and-refining-common-algorithms","title":{"rendered":"Template Method Pattern: \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc744 \ucd94\uc0c1\ud654\ud558\uace0 \uad6c\uccb4\ud654\ud558\uc5ec \ucf54\ub4dc \uc7ac\uc0ac\uc6a9\uc131 \ub192\uc774\uae30"},"content":{"rendered":"

\ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc740 \uc18c\ud504\ud2b8\uc6e8\uc5b4 \uac1c\ubc1c\uc5d0\uc11c \uc790\uc8fc \uc0ac\uc6a9\ub418\ub294 \ub514\uc790\uc778 \ud328\ud134 \uc911 \ud558\ub098\uc785\ub2c8\ub2e4. \uc774 \ud328\ud134\uc740 \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc744 \ucd94\uc0c1\ud654\ud558\uace0 \uad6c\uccb4\ud654\ud558\uc5ec \ucf54\ub4dc \uc7ac\uc0ac\uc6a9\uc131\uc744 \ub192\uc774\ub294 \ub370\uc5d0 \uc0ac\uc6a9\ub429\ub2c8\ub2e4. \uc774\ubc88 \uae00\uc5d0\uc11c\ub294 \ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc744 \uc790\uc138\ud788 \uc0b4\ud3b4\ubcf4\uace0, \uc774 \ud328\ud134\uc744 \uc0ac\uc6a9\ud558\uc5ec \uc54c\uace0\ub9ac\uc998 \uc911\ubcf5\uc744 \uc81c\uac70\ud558\ub294 \ubc29\ubc95\uc744 \uc0b4\ud3b4\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.<\/p>\n

\ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134: \ucd94\uc0c1\ud654\uc640 \uad6c\uccb4\ud654\ub85c \ucf54\ub4dc \uc7ac\uc0ac\uc6a9\uc131 \ub192\uc774\uae30<\/h2>\n

\ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc740 \uac1d\uccb4\uc9c0\ud5a5 \ub514\uc790\uc778 \ud328\ud134 \uc911 \ud558\ub098\ub85c, \uc0c1\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \ucc98\ub9ac\uc758 \uace8\uaca9\uc744 \uc815\uc758\ud558\uace0 \ud558\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \ucc98\ub9ac\uc758 \uad6c\uccb4\uc801\uc778 \ub0b4\uc6a9\uc744 \uacb0\uc815\ud558\ub294 \ud328\ud134\uc785\ub2c8\ub2e4. \uc774 \ud328\ud134\uc740 \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc744 \ucd94\uc0c1\ud654\ud558\uace0 \uad6c\uccb4\ud654\ud558\uc5ec \ucf54\ub4dc \uc7ac\uc0ac\uc6a9\uc131\uc744 \ub192\uc774\uba70, \uc720\uc9c0\ubcf4\uc218\uc131\uacfc \uac00\ub3c5\uc131\uc744 \ub192\uc77c \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\uc608\ub97c \ub4e4\uc5b4, \ud30c\uc77c \uc0dd\uc131\uc5d0 \ub300\ud55c \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc774 \uc788\ub2e4\uace0 \uac00\uc815\ud574\ubcf4\uaca0\uc2b5\ub2c8\ub2e4. \uc774 \uacbd\uc6b0, \ud30c\uc77c \uc0dd\uc131\uc758 \uace8\uaca9\uc744 \uc815\uc758\ud558\ub294 \ucd94\uc0c1 \ud074\ub798\uc2a4\ub97c \ub9cc\ub4e4\uace0, \uad6c\uccb4\uc801\uc778 \ud30c\uc77c \uc0dd\uc131 \ubc29\ubc95\uc740 \uc0c1\uc18d\ubc1b\uc740 \ud558\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \uad6c\ud604\ud558\ub3c4\ub85d \ud569\ub2c8\ub2e4. \uc774\ub807\uac8c \ud558\uba74 \ud30c\uc77c \uc0dd\uc131 \uacfc\uc815\uc5d0\uc11c \uc911\ubcf5\ub418\ub294 \ucf54\ub4dc\ub97c \ucd5c\uc18c\ud654\ud560 \uc218 \uc788\uc73c\uba70, \ub2e4\uc591\ud55c \ud558\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c \uc774 \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\uc544\ub798\ub294 Java \ucf54\ub4dc\ub85c \ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc744 \uad6c\ud604\ud55c \uc608\uc2dc\uc785\ub2c8\ub2e4. \ucd94\uc0c1 \ud074\ub798\uc2a4\uc778 AbstractClass<\/code>\uc5d0\uc11c templateMethod()<\/code>\ub97c \uc815\uc758\ud558\uace0, \uc774\ub97c \uc0c1\uc18d\ubc1b\uc740 ConcreteClass<\/code>\uc5d0\uc11c primitiveOperation()<\/code>\uc744 \uc624\ubc84\ub77c\uc774\ub529\ud558\uc5ec \uad6c\uccb4\ud654\ud569\ub2c8\ub2e4.<\/p>\n

abstract class AbstractClass {\n    public void templateMethod() {\n        \/\/ \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc758 \uace8\uaca9 \uc815\uc758\n        primitiveOperation();\n    }\n    protected abstract void primitiveOperation();\n}\n\nclass ConcreteClass extends AbstractClass {\n    protected void primitiveOperation() {\n        \/\/ \uad6c\uccb4\uc801\uc778 \ucc98\ub9ac \ub0b4\uc6a9 \uad6c\ud604\n    }\n}<\/code><\/pre>\n

\ud6a8\uc728\uc801\uc778 \ub514\uc790\uc778 \ud328\ud134, \ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc\ub85c \uc54c\uace0\ub9ac\uc998 \uc911\ubcf5 \uc81c\uac70\ud558\uae30<\/h2>\n

\ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc744 \uc0ac\uc6a9\ud558\uba74 \uc54c\uace0\ub9ac\uc998 \uc911\ubcf5\uc744 \uc81c\uac70\ud560 \uc218 \uc788\uc73c\uba70, \uc774\ub97c \ud1b5\ud574 \ucf54\ub4dc\uc758 \uc7ac\uc0ac\uc6a9\uc131\uacfc \uac00\ub3c5\uc131\uc744 \ub192\uc77c \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774 \ud328\ud134\uc740 \ud2b9\ud788 \ub300\uaddc\ubaa8 \uc18c\ud504\ud2b8\uc6e8\uc5b4 \uac1c\ubc1c\uc5d0\uc11c \ud6a8\uc728\uc801\uc778 \ub514\uc790\uc778 \ud328\ud134\uc73c\ub85c \uc0ac\uc6a9\ub429\ub2c8\ub2e4.<\/p>\n

\ub610\ud55c, \ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc740 \ub2e4\ub978 \ub514\uc790\uc778 \ud328\ud134\uacfc \ud568\uaed8 \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4, \ud329\ud1a0\ub9ac \uba54\uc11c\ub4dc \ud328\ud134\uacfc \ud568\uaed8 \uc0ac\uc6a9\ud558\uba74 \uac1d\uccb4 \uc0dd\uc131\uc5d0 \ub300\ud55c \uc911\ubcf5 \ucf54\ub4dc\ub97c \ucd5c\uc18c\ud654\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

abstract class AbstractFactory {\n    public abstract Product createProduct();\n    public void process() {\n        Product product = createProduct();\n        \/\/ product \ucc98\ub9ac\n    }\n}<\/code><\/pre>\n

\uc704 \ucf54\ub4dc\uc5d0\uc11c AbstractFactory<\/code>\ub294 \ud329\ud1a0\ub9ac \uba54\uc11c\ub4dc \ud328\ud134\uc744 \uad6c\ud604\ud55c \ud074\ub798\uc2a4\uc785\ub2c8\ub2e4. \uc774 \ud074\ub798\uc2a4\uc5d0\uc11c process()<\/code>\uc5d0\uc11c\ub294 createProduct()<\/code>\uc744 \ud638\ucd9c\ud558\uc5ec Product<\/code> \uac1d\uccb4\ub97c \uc0dd\uc131\ud558\uace0, \uc774\ub97c \ucc98\ub9ac\ud569\ub2c8\ub2e4. \ub530\ub77c\uc11c, AbstractFactory<\/code>\ub97c \uc0c1\uc18d\ubc1b\uc740 \ud558\uc704 \ud074\ub798\uc2a4\uc5d0\uc11c createProduct()<\/code>\uc744 \uc624\ubc84\ub77c\uc774\ub529\ud558\uc5ec \uad6c\uccb4\ud654\ud560 \uacbd\uc6b0, \ub2e4\uc591\ud55c Product<\/code> \uac1d\uccb4\ub97c \uc0dd\uc131\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc740 \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc744 \ucd94\uc0c1\ud654\ud558\uace0 \uad6c\uccb4\ud654\ud560 \uc218 \uc788\ub294 \ud6a8\uc728\uc801\uc778 \ub514\uc790\uc778 \ud328\ud134 \uc911 \ud558\ub098\uc785\ub2c8\ub2e4. \uc774 \ud328\ud134\uc744 \uc0ac\uc6a9\ud558\uba74 \uc54c\uace0\ub9ac\uc998 \uc911\ubcf5\uc744 \uc81c\uac70\ud558\uc5ec \ucf54\ub4dc\uc758 \uc7ac\uc0ac\uc6a9\uc131\uacfc \uac00\ub3c5\uc131\uc744 \ub192\uc77c \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub610\ud55c, \ud15c\ud50c\ub9bf \uba54\uc11c\ub4dc \ud328\ud134\uc740 \ub2e4\ub978 \ub514\uc790\uc778 \ud328\ud134\uacfc \ud568\uaed8 \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\uc73c\uba70, \uc774\ub97c \ud1b5\ud574 \ucf54\ub4dc\uc758 \uc720\uc9c0\ubcf4\uc218\uc131\uacfc \ud655\uc7a5\uc131\uc744 \ub192\uc77c \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub530\ub77c\uc11c, \uc18c\ud504\ud2b8\uc6e8\uc5b4 \uac1c\ubc1c\uc5d0\uc11c \ud6a8\uc728\uc801\uc778 \ub514\uc790\uc778 \ud328\ud134\uc73c\ub85c \uc0ac\uc6a9\ub429\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"

Template Method Pattern: \uacf5\ud1b5\ub41c \uc54c\uace0\ub9ac\uc998\uc744 \ucd94\uc0c1\ud654\ud558\uace0 \uad6c\uccb4\ud654\ud558\uc5ec \ucf54\ub4dc \uc7ac\uc0ac\uc6a9\uc131 \ub192\uc774\uae30<\/p>\n","protected":false},"author":1,"featured_media":12633,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[2015,2500,2523,2200],"class_list":["post-4421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-development","tag-method","tag-pattern","tag-uncategorized"],"acf":[],"_links":{"self":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/4421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/comments?post=4421"}],"version-history":[{"count":0,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/4421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media\/12633"}],"wp:attachment":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media?parent=4421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/categories?post=4421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/tags?post=4421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}