{"id":36192,"date":"2023-06-04T11:17:28","date_gmt":"2023-06-04T02:17:28","guid":{"rendered":"https:\/\/m9js.shop\/blog\/?p=36192"},"modified":"2023-06-04T11:17:28","modified_gmt":"2023-06-04T02:17:28","slug":"java-design-patterns-managing-tree-structures-of-objects-with-the-composite-pattern","status":"publish","type":"post","link":"https:\/\/m9js.shop\/blog\/development\/java-design-patterns-managing-tree-structures-of-objects-with-the-composite-pattern","title":{"rendered":"\uc790\ubc14 \ub514\uc790\uc778 \ud328\ud134: \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc73c\ub85c \uac1d\uccb4\ub4e4\uc758 \ud2b8\ub9ac \uad6c\uc870 \uad00\ub9ac"},"content":{"rendered":"

\uc790\ubc14 \ub514\uc790\uc778 \ud328\ud134 \uac1c\uc694<\/h1>\n

\uc18c\ud504\ud2b8\uc6e8\uc5b4 \uac1c\ubc1c\uc5d0\uc11c \ub514\uc790\uc778 \ud328\ud134\uc740 \uc7ac\uc0ac\uc6a9\uc131\uacfc \uc720\uc9c0\ubcf4\uc218\uc131\uc744 \ub192\uc774\ub294 \ub370 \uc911\uc694\ud55c \uc5ed\ud560\uc744 \ud569\ub2c8\ub2e4. \ub514\uc790\uc778 \ud328\ud134\uc740 \uad6c\ud604\ud560 \ubb38\uc81c\uc5d0 \ub300\ud574 \uc77c\ubc18\uc801\uc73c\ub85c \uc0ac\uc6a9\ub418\ub294 \ud574\uacb0\ucc45\uc744 \uc81c\uacf5\ud558\uba70, \uc774\ub97c \uc774\uc6a9\ud558\uc5ec \uc18c\ud504\ud2b8\uc6e8\uc5b4 \uc124\uacc4\ub97c \ubcf4\ub2e4 \uc27d\uace0 \uc720\uc5f0\ud558\uac8c \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub514\uc790\uc778 \ud328\ud134\uc740 GoF(Gang of Four)\ub77c \ubd88\ub9ac\ub294 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides\uc758 \uc800\uc11c "Design Patterns: Elements of Reusable Object-Oriented Software"\uc5d0\uc11c \ucc98\uc74c \uc18c\uac1c\ub418\uc5c8\uc2b5\ub2c8\ub2e4.<\/p>\n

\uc790\ubc14\ub294 \uac1d\uccb4 \uc9c0\ud5a5 \ud504\ub85c\uadf8\ub798\ubc0d \uc5b8\uc5b4\uc774\uba70, \uac1d\uccb4 \uc9c0\ud5a5 \ub514\uc790\uc778 \ud328\ud134\uc744 \uc9c0\uc6d0\ud569\ub2c8\ub2e4. \uc790\ubc14 \ub514\uc790\uc778 \ud328\ud134\uc740 \uc18c\ud504\ud2b8\uc6e8\uc5b4 \uac1c\ubc1c\uc5d0\uc11c \uc790\uc8fc \uc0ac\uc6a9\ub418\uba70, \uac1d\uccb4 \uc9c0\ud5a5 \ud504\ub85c\uadf8\ub798\ubc0d \uac1c\ub150\uc744 \ud65c\uc6a9\ud558\uc5ec \ubb38\uc81c\ub97c \ud574\uacb0\ud558\ub294 \ub370 \uc720\uc6a9\ud569\ub2c8\ub2e4.<\/p>\n

\uc774\ubc88 \uae00\uc5d0\uc11c\ub294 \uc790\ubc14 \ub514\uc790\uc778 \ud328\ud134 \uc911\uc5d0\uc11c\ub3c4 \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.<\/p>\n

\ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc758 \uc774\ud574\uc640 \ud65c\uc6a9<\/h1>\n

\ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc740 \uad6c\uc870 \ud328\ud134 \uc911 \ud558\ub098\ub85c, \uac1d\uccb4\ub4e4\uc744 \ud2b8\ub9ac \uad6c\uc870\ub85c \uad6c\uc131\ud558\uc5ec \uc804\uccb4-\ubd80\ubd84 \uad00\uacc4\ub97c \ud45c\ud604\ud558\ub294 \ub370 \uc0ac\uc6a9\ub429\ub2c8\ub2e4. \uc774 \ud328\ud134\uc740 \ub2e8\uc77c \uac1d\uccb4\uc640 \uac1d\uccb4\uc758 \uc9d1\ud569\uc744 \ub3d9\uc77c\ud55c \ubc29\uc2dd\uc73c\ub85c \ub2e4\ub8f0 \uc218 \uc788\uac8c \ud574\uc8fc\uba70, \uc804\uccb4\uc640 \ubd80\ubd84\uc744 \ubaa8\ub450 \ub098\ud0c0\ub0b4\ub294 \uac1d\uccb4\ub97c \ub9cc\ub4e4 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc758 \ud575\uc2ec \uc544\uc774\ub514\uc5b4\ub294 "\uc804\uccb4-\ubd80\ubd84" \uad00\uacc4\ub97c \ub098\ud0c0\ub0b4\ub294 \uac83\uc785\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4, "\ucef4\ud4e8\ud130"\ub77c\ub294 \uc804\uccb4 \uac1d\uccb4\ub294 "CPU", "\uba54\ubaa8\ub9ac", "\ud558\ub4dc\ub514\uc2a4\ud06c" \ub4f1\uc758 \ubd80\ubd84 \uac1d\uccb4\ub4e4\ub85c \uad6c\uc131\ub429\ub2c8\ub2e4. \uc774\ub7ec\ud55c \uac1d\uccb4\ub4e4\uc740 \ub2e4\uc2dc "\uc804\uc6d0", "\ucfe8\ub7ec" \ub4f1\uc758 \ud558\uc704 \ubd80\ubd84 \uac1d\uccb4\ub4e4\ub85c \uad6c\uc131\ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub7ec\ud55c \uac1d\uccb4\ub4e4\uc740 \ubaa8\ub450 \ub3d9\uc77c\ud55c \uc778\ud130\ud398\uc774\uc2a4\ub97c \uacf5\uc720\ud558\uba70, \uac01 \uac1d\uccb4\ub294 \uc790\uc2e0\uc758 \ubd80\ubd84 \uac1d\uccb4\ub97c \uac00\uc9c8 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

\ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc740 \uac1d\uccb4 \uc9c0\ud5a5 \ub514\uc790\uc778\uc5d0\uc11c \ub9ce\uc774 \uc0ac\uc6a9\ub418\uba70, \ud2b9\ud788 GUI \ud504\ub85c\uadf8\ub798\ubc0d, \ud30c\uc77c \uc2dc\uc2a4\ud15c \ub4f1\uc5d0\uc11c \ub9ce\uc774 \ud65c\uc6a9\ub429\ub2c8\ub2e4. \uc774 \ud328\ud134\uc744 \uc0ac\uc6a9\ud558\uba74 \uc804\uccb4 \uac1d\uccb4\uc640 \ubd80\ubd84 \uac1d\uccb4\ub4e4 \uac04\uc758 \uad00\uacc4\ub97c \uc27d\uac8c \ub2e4\ub8f0 \uc218 \uc788\uc73c\uba70, \uac1d\uccb4\ub97c \uad6c\uc131\ud558\ub294 \ubc29\uc2dd\uc774 \uc720\uc5f0\ud574\uc9d1\ub2c8\ub2e4.<\/p>\n

\uac1d\uccb4\ub4e4\uc758 \ud2b8\ub9ac \uad6c\uc870 \uad00\ub9ac \ubc29\ubc95<\/h1>\n

\ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc744 \uc0ac\uc6a9\ud558\uc5ec \uac1d\uccb4\ub4e4\uc758 \ud2b8\ub9ac \uad6c\uc870\ub97c \uad00\ub9ac\ud558\ub294 \ubc29\ubc95\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n

    \n
  1. Component \uc778\ud130\ud398\uc774\uc2a4 \uc815\uc758<\/li>\n<\/ol>\n

    \ud2b8\ub9ac \uad6c\uc870\uc758 \ubaa8\ub4e0 \uac1d\uccb4\ub294 Component \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774 \uc778\ud130\ud398\uc774\uc2a4\ub294 \uc804\uccb4-\ubd80\ubd84 \uad00\uacc4\ub97c \ub098\ud0c0\ub0b4\uba70, \ubaa8\ub4e0 \uac1d\uccb4\uac00 \uacf5\ud1b5\uc73c\ub85c \uac00\uc9c0\ub294 \uba54\uc11c\ub4dc\ub97c \uc815\uc758\ud569\ub2c8\ub2e4.<\/p>\n

      \n
    1. Leaf \ud074\ub798\uc2a4\uc640 Composite \ud074\ub798\uc2a4 \uad6c\ud604<\/li>\n<\/ol>\n

      Component \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud558\ub294 \ub450 \uac00\uc9c0 \ud074\ub798\uc2a4\uac00 \ud544\uc694\ud569\ub2c8\ub2e4. \uccab \ubc88\uc9f8\ub294 Leaf \ud074\ub798\uc2a4\ub85c, \uc774 \ud074\ub798\uc2a4\ub294 \uc790\uc2dd \uac1d\uccb4\ub97c \uac00\uc9c0\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4. \ub450 \ubc88\uc9f8\ub294 Composite \ud074\ub798\uc2a4\ub85c, \uc774 \ud074\ub798\uc2a4\ub294 \uc790\uc2dd \uac1d\uccb4\ub97c \uac00\uc9c8 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

        \n
      1. Composite \uac1d\uccb4\uc5d0\uc11c \uc790\uc2dd \uac1d\uccb4 \uad00\ub9ac<\/li>\n<\/ol>\n

        Composite \uac1d\uccb4\ub294 \uc790\uc2dd \uac1d\uccb4\ub97c \uac00\uc9c0\ub294 \uacbd\uc6b0, \uc790\uc2dd \uac1d\uccb4\ub4e4\uc744 \uad00\ub9ac\ud560 \uc218 \uc788\ub294 \uba54\uc11c\ub4dc\ub97c \uc81c\uacf5\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774 \uba54\uc11c\ub4dc\ub294 \uc790\uc2dd \uac1d\uccb4\ub97c \ucd94\uac00, \uc81c\uac70, \uc870\ud68c\ud560 \uc218 \uc788\ub294 \uae30\ub2a5\uc744 \uc81c\uacf5\ud569\ub2c8\ub2e4.<\/p>\n

          \n
        1. \uc804\uccb4-\ubd80\ubd84 \uad00\uacc4 \ub2e4\ub8e8\uae30<\/li>\n<\/ol>\n

          \ud2b8\ub9ac \uad6c\uc870\uc5d0\uc11c \uc804\uccb4 \uac1d\uccb4\uc640 \ubd80\ubd84 \uac1d\uccb4\ub294 \ub3d9\uc77c\ud55c \uc778\ud130\ud398\uc774\uc2a4\ub97c \uacf5\uc720\ud569\ub2c8\ub2e4. \ub530\ub77c\uc11c \uc804\uccb4 \uac1d\uccb4\uc5d0\uc11c \ubd80\ubd84 \uac1d\uccb4\ub97c \ub2e4\ub8f0 \ub54c\ub294 \ub3d9\uc77c\ud55c \uc778\ud130\ud398\uc774\uc2a4\ub97c \uc0ac\uc6a9\ud558\uc5ec \ub2e4\ub8f0 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

          \uc790\ubc14\uc5d0\uc11c\uc758 \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134 \uad6c\ud604 \uc608\uc81c<\/h1>\n

          \ub2e4\uc74c\uc740 \uc790\ubc14\uc5d0\uc11c \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc744 \uad6c\ud604\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n

          interface Component {\n    public void operation();\n}\n\nclass Leaf implements Component {\n    public void operation() {\n        \/\/ Leaf \uac1d\uccb4\uc758 \ub3d9\uc791\uc744 \uc815\uc758\ud569\ub2c8\ub2e4.\n    }\n}\n\nclass Composite implements Component {\n    private List children = new ArrayList();\n    public void add(Component component) {\n        children.add(component);\n    }\n    public void remove(Component component) {\n        children.remove(component);\n    }\n    public void operation() {\n        \/\/ Composite \uac1d\uccb4\uc758 \ub3d9\uc791\uc744 \uc815\uc758\ud569\ub2c8\ub2e4.\n        for (Component component : children) {\n            component.operation();\n        }\n    }\n}<\/code><\/pre>\n

          \uc704 \uc608\uc81c\uc5d0\uc11c\ub294 Component \uc778\ud130\ud398\uc774\uc2a4\ub97c \uad6c\ud604\ud55c Leaf \ud074\ub798\uc2a4\uc640 Composite \ud074\ub798\uc2a4\ub97c \uc815\uc758\ud558\uc600\uc2b5\ub2c8\ub2e4. Composite \ud074\ub798\uc2a4\ub294 \uc790\uc2dd \uac1d\uccb4\ub97c \uac00\uc9c8 \uc218 \uc788\uc73c\uba70, \uc790\uc2dd \uac1d\uccb4\ub97c \ucd94\uac00\ud558\uac70\ub098 \uc81c\uac70\ud560 \uc218 \uc788\ub294 \uba54\uc11c\ub4dc\ub97c \uc81c\uacf5\ud569\ub2c8\ub2e4. operation() \uba54\uc11c\ub4dc\ub294 \ubaa8\ub4e0 \uac1d\uccb4\uac00 \uacf5\ud1b5\uc73c\ub85c \uac00\uc9c0\ub294 \uba54\uc11c\ub4dc\ub85c, \uac1d\uccb4\uc758 \ub3d9\uc791\uc744 \uc815\uc758\ud569\ub2c8\ub2e4.<\/p>\n

          \uc774\uc81c \uc704 \uc608\uc81c\ub97c \uc774\uc6a9\ud558\uc5ec \uac1d\uccb4\ub4e4\uc758 \ud2b8\ub9ac \uad6c\uc870\ub97c \uad6c\uc131\ud574\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.<\/p>\n

          Component root = new Composite();\nComponent leaf1 = new Leaf();\nComponent leaf2 = new Leaf();\nComponent composite = new Composite();\nComponent leaf3 = new Leaf();\n\n((Composite) root).add(leaf1);\n((Composite) root).add(composite);\n((Composite) composite).add(leaf2);\n((Composite) composite).add(leaf3);\n\nroot.operation();<\/code><\/pre>\n

          \uc704 \uc608\uc81c\uc5d0\uc11c\ub294 root \uac1d\uccb4\ub97c Composite \ud074\ub798\uc2a4\ub85c \uc0dd\uc131\ud558\uc600\uc2b5\ub2c8\ub2e4. \uc774\ud6c4\uc5d0\ub294 Leaf \uac1d\uccb4\uc640 Composite \uac1d\uccb4\ub97c \uc0dd\uc131\ud558\uc5ec root \uac1d\uccb4\uc5d0 \ucd94\uac00\ud558\uc600\uc2b5\ub2c8\ub2e4. \ub9c8\uc9c0\ub9c9\uc73c\ub85c root \uac1d\uccb4\uc758 operation() \uba54\uc11c\ub4dc\ub97c \ud638\ucd9c\ud558\uc5ec \uc804\uccb4 \uac1d\uccb4\uc758 \ub3d9\uc791\uc744 \uc2e4\ud589\ud569\ub2c8\ub2e4.<\/p>\n

          \uc774\ubc88 \uae00\uc5d0\uc11c\ub294 \uc790\ubc14 \ub514\uc790\uc778 \ud328\ud134 \uc911 \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc5d0 \ub300\ud574 \uc54c\uc544\ubcf4\uc558\uc2b5\ub2c8\ub2e4. \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc740 \uac1d\uccb4\ub4e4\uc758 \ud2b8\ub9ac \uad6c\uc870\ub97c \uad00\ub9ac\ud558\ub294 \ub370 \uc720\uc6a9\ud55c \ud328\ud134\uc73c\ub85c, \uac1d\uccb4 \uc9c0\ud5a5 \ub514\uc790\uc778\uc5d0\uc11c \ub9ce\uc774 \uc0ac\uc6a9\ub429\ub2c8\ub2e4. \uc790\ubc14\uc5d0\uc11c\ub294 \uc774 \ud328\ud134\uc744 \uc27d\uac8c \uad6c\ud604\ud560 \uc218 \uc788\uc73c\uba70, \uc774\ub97c \ud65c\uc6a9\ud558\uc5ec \uc720\uc5f0\ud558\uace0 \uc7ac\uc0ac\uc6a9 \uac00\ub2a5\ud55c \uc18c\ud504\ud2b8\uc6e8\uc5b4\ub97c \uac1c\ubc1c\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"

          \uc790\ubc14 \ub514\uc790\uc778 \ud328\ud134: \ucef4\ud3ec\uc9c0\ud2b8 \ud328\ud134\uc73c\ub85c \uac1d\uccb4\ub4e4\uc758 \ud2b8\ub9ac \uad6c\uc870 \uad00\ub9ac.<\/p>\n","protected":false},"author":1,"featured_media":33704,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[2703,2131,2698,2188,2066,5925,5979,3414],"class_list":["post-36192","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-composite","tag-design","tag-object","tag-public","tag-software","tag-5925","tag-5979","tag-3414"],"acf":[],"_links":{"self":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/36192","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=36192"}],"version-history":[{"count":1,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/36192\/revisions"}],"predecessor-version":[{"id":36331,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/36192\/revisions\/36331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media\/33704"}],"wp:attachment":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media?parent=36192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/categories?post=36192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/tags?post=36192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}