1.1. Intent ¶
๋ถ๋ถ-์ ์ฒด ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ํํํ๊ธฐ ์ํด ๊ฐ์ฒด๋ค์ ํธ๋ฆฌ๊ตฌ์กฐ๋ก ๊ตฌ์ฑํ๋ค. Composite ๋ ํด๋ผ์ด์ธํธ๋ก ํ์ฌ๊ธ ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ค๊ณผ ๊ฐ์ฒด์ ๋ฌถ์์ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ์ฒ๋ฆฌํ๊ฒ ํด์ค๋ค.
1.2. Motivation ¶
๋๋ก์ฐ ์๋ํฐ๋ ํ๋ก์ค๊ณ ์์คํ
๊ณผ ๊ฐ์ ๊ทธ๋ํฝ ์ดํ๋ฆฌ์ผ์ด์
์ ๋จ์ํ ์ปดํฌ๋ํธ๋ค์ ์ฐจ์์ ๋์ด์ ๋ณต์กํ ๋ํ๋ค์ ๋ง๋ค์ด๋ด๋๋ฐ ์ด์ฉ๋๋ค. ์ฌ์ฉ์๋ ๋ ํฐ ์ปดํฌ๋ํธ๋ค์ ํ์ฑํ๊ธฐ ์ํด ์ปดํฌ๋ํธ๋ค์ ๊ทธ๋ฃนํํ ์ ์๊ณ , ๋ ํฐ ์ปดํฌ๋ํธ๋ค์ ํ์ฑํ๊ธฐ ์ํด ๋ ๊ทธ๋ฃนํ ํ ์ ์๋ค. ๋จ์ํ ๊ตฌํ๋ฐฉ๋ฒ์ผ๋ก๋ Text ๋ Line ๊ฐ์ ๊ทธ๋ํฝ์ ๊ธฐ๋ณธ์์๋ค์ ๋ํ ํด๋์ค๋ค์ ์ ์ํ ๋ค, ์ด๋ฌํ ๊ธฐ๋ณธ์์๋ค์ ๋ํด ์ปจํ
์ด๋ ์ญํ ์ ํ๋ ๋ค๋ฅธ ํด๋์ค์ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
ํ์ง๋ง, ์ด๋ฌํ ์ ๊ทผ๋ฐฉ๋ฒ์๋ ๋ฌธ์ ์ ์ด ์๋ค. ๋น๋ก ๋๋ถ๋ถ์ ์๊ฐ๋์ ์ฌ์ฉ์๊ฐ ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ค์ ๋์ผํ๊ฒ ์ทจ๊ธํ๋ค ํ๋๋ผ๋, ์ด๋ฌํ ํด๋์ค๋ค์ ์ด์ฉํ๋ ์ฝ๋๋ ๋ฐ๋์ ๊ธฐ๋ณธ๊ฐ์ฒด์ ์ปจํ
์ด๋ ๊ฐ์ฒด๋ฅผ ๋ค๋ฅด๊ฒ ์ทจ๊ธํ์ฌ ์ฝ๋ฉํด์ผ ํ๋ค๋ ์ ์ด๋ค. ์ด๋ฌํ ๊ฐ์ฒด๋ค์ ๊ตฌ๋ณ์ ์ดํ๋ฆฌ์ผ์ด์
์ ๋ณต์กํ๊ฒ ๋ง๋ ๋ค. CompositePattern์ ๊ฐ์ฒด๋ค์ ๋ํ ์ฌ๊ท์ ์กฐํฉ ๋ฐฉ๋ฒ์ ์์ ํจ์ผ๋ก์, ํด๋ผ์ด์ธํธ๋ค๋ก ํ์ฌ๊ธ ์ด๋ฌํ ๊ตฌ๋ถ์ ํ ํ์๊ฐ ์๋๋ก ํด์ค๋ค.
CompositePattern์ ํต์ฌ์ ๊ธฐ๋ณธ์์๋ค๊ณผ ๊ธฐ๋ณธ์์๋ค์ ์ปจํ
์ด๋๋ฅผ ๋ ๋ค ํํํ๋ ์ถ์ ํด๋์ค์ ์๋ค. ๊ทธ๋ํฝ ์์คํ
์์ ์ฌ๊ธฐ Graphic class๋ฅผ ์๋ก ๋ค ์ ์๊ฒ ๋ค. Graphic ์ Draw ์ ๊ฐ์ ๊ทธ๋ํฝ ๊ฐ์ฒด๋ค์ ๊ตฌ์ฒดํํ๋ ๋ช
๋ น๋ค์ ์ ์ธํ๋ค. ๋ํ Graphic ์ Graphic ์ ์์ํด๋์ค๋ค (tree ๊ตฌ์กฐ์์์ parent-child ๊ด๊ณ)์ ๋ํด ์ ๊ทผํ๊ณ ๊ด๋ฆฌํ๋ ๋ช
๋ น๋ค๊ณผ ๊ฐ์ ๋ชจ๋ composite ๊ฐ์ฒด๋ค์ด ๊ณต์ ํ๋ ๋ช
๋ น์ด๋ค์ ์ ์ธํ๋ค.
Line, Rectangle, Text ์ ๊ฐ์ ์๋ธ ํด๋์ค๋ค์ (์์ class diagram ์ฐธ์กฐ) ๊ธฐ๋ณธ ๊ทธ๋ํฝ ๊ฐ์ฒด๋ค์ ์ ์ํ๋ค. ์ด๋ฌํ ํด๋์ค๋ค์ ๊ฐ๊ฐ ์ ์ด๋ ์ฌ๊ฐํ, ํ
์คํธ๋ฅผ ๊ทธ๋ฆฌ๋ 'Draw' operation์ ๊ตฌํํ๋ค. ๊ธฐ๋ณธ์ ์ธ ๊ทธ๋ํฝ ๊ตฌ์ฑ์์๋ค์ ์์์์๋ค์ ๊ฐ์ง์ง ์์ผ๋ฏ๋ก, ์ด ์๋ธ ํด๋์ค๋ค์ ์์์์๊ณผ ๊ด๋ จ๋ ๋ช
๋ น๋ค์ ๊ตฌํํ์ง ์๋๋ค.
Picture ํด๋์ค๋ Graphic ๊ฐ์ฒด์ ๋ํ ํฌํจ๊ด๊ณ๋ฅผ ์ ์ํ๋ค. Picture ๋ Picture์ children์ Draw ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฒ๋ Draw ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค. ๊ทธ๋ฆฌ๊ณ Picture๋ child์ ๊ด๋ จํ ๋ช
๋ น์ด๋ค์ ์๋ง๊ฒ ๊ตฌํํ๋ค. Picture ์ธํฐํ์ด์ค๋ Graphic์ ์ธํฐํ์ด์ค๋ฅผ ํ์ฑํ๋ฏ๋ก, Picture ๊ฐ์ฒด๋ ๋ค๋ฅธ Pricture๋ค์ ์ฌ๊ท์ ์ผ๋ก ์กฐํฉํ ์ ์๋ค.
๋ค์ ๋ค์ด์ด๊ทธ๋จ์ ์ฌ๊ท์ ์ผ๋ก Graphic ๊ฐ์ฒด๋ค์ด ์กฐํฉ๋ ์ผ๋ฐ์ ์ธ composite ๊ฐ์ฒด์ ๊ตฌ์กฐ๋ฅผ ๋ณด์ฌ์ค๋ค.
1.3. Applicability ¶
๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ CompositePattern ์ ์ด์ฉํ ์ ์๋ค.
- ๊ฐ์ฒด๋ค์ ๋ถ๋ถ-์ ์ฒด ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ํํํ๊ณ ์ถ์๋
- ํด๋ผ์ด์ธํธ๋ค์ด ๊ฐ๊ฐ๋ณ ๊ฐ์ฒด๋ค๊ณผ ๊ฐ์ฒด๋ค์ ์กฐํฉ๋ค์ ์ฐจ์ด์ ์ ์ ๊ฒฝ์ฐ์ง ์๋๋ก ํ๊ณ ์ถ์ ๊ฒฝ์ฐ. Composite๋ฅผ ์ด์ฉํจ์ผ๋ก์จ, ํด๋ผ์ด์ธํธ๋ค์ composite ๊ตฌ์กฐ์ ๋ชจ๋ ๊ฐ์ฒด๋ค์ ๋๋ฑํ๊ฒ ์ทจ๊ธํ ๊ฒ์ด๋ค.
1.4. Structure ¶
A typical Composite object structure might look like this:
์ผ๋ฐ์ ์ธ Composite ๊ฐ์ฒด๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ ๋ชจ์ต์ ๋๋ค.
์ผ๋ฐ์ ์ธ Composite ๊ฐ์ฒด๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ ๋ชจ์ต์ ๋๋ค.
1.5. Participants ¶
- Component (Graphic)
- composition ์์ ๊ฐ์ฒด๋ค์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ค.
- ๋ชจ๋ ํด๋์ค๋ค์์ ๊ธฐ๋ณธ์ ์ธ ์ธํฐํ์ด์ค์ ๋ํ ๊ธฐ๋ณธ ํ์๋ฅผ ๊ตฌํํ๋ค.
- ์์ ์ปดํฌ๋ํธ๋ค์ ์ ๊ทผํ๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ค.
- (optional) ์ฌ๊ท ๊ตฌ์กฐ์์์ ํด๋น ์ปดํฌ๋ํธ์ ๋ถ๋ชจ ํด๋์ค๋ก ์ ๊ทผํ๊ธฐ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๊ณ , ์ ์ ํ๊ฒ ๊ตฌํํ๋ค.
- composition ์์ ๊ฐ์ฒด๋ค์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ธํ๋ค.
- Leaf (Rectangle, Line, Text, etc.)
- composition์ leaf ๊ฐ์ฒด๋ฅผ ํํํ๋ค. leaf ๋ children๋ฅผ ๊ฐ์ง์ง ์๋๋ค.
- composition ์ ๊ธฐ๋ณธ ๊ฐ์ฒด๋ค์ ํ์๋ฅผ ์ ์ํ๋ค.
- composition์ leaf ๊ฐ์ฒด๋ฅผ ํํํ๋ค. leaf ๋ children๋ฅผ ๊ฐ์ง์ง ์๋๋ค.
- Composite (Picture)
- children์ ๊ฐ์ง๋ ์ปดํฌ๋ํธ๋ค์ ์ํ ํ์๋ฅผ ์ ์ํ๋ค.
- ์์ ์ปดํฌ๋ํธ๋ค์ ์ ์ฅํ๋ค.
- Component ์ธํฐํ์ด์ค์ค ์์์ปดํฌ๋ํธ ๊ด๋ จ ๋ช
๋ น๋ค์ ๊ตฌํํ๋ค.
- children์ ๊ฐ์ง๋ ์ปดํฌ๋ํธ๋ค์ ์ํ ํ์๋ฅผ ์ ์ํ๋ค.
- Client
- Component์ ์ธํฐํ์ด์ค๋ฅผ ํตํด composition์ ๊ฐ์ฒด๋ค์ ์กฐ์ํ๋ค.
- Component์ ์ธํฐํ์ด์ค๋ฅผ ํตํด composition์ ๊ฐ์ฒด๋ค์ ์กฐ์ํ๋ค.
1.6. Collaborations ¶
- ํด๋ผ์ด์ธํธ๋ค์ Component ํด๋์ค์ ์ธํฐํ์ด์ค๋ฅผ ์ด์ฉ, composite ๊ตฌ์กฐ์ ๊ฐ์ฒด๋ค๊ณผ ์ํธ์์ฉ์ ํ๋ค. ๋ง์ผ ์ํธ์์ฉํ๋ ๊ฐ์ฒด๊ฐ Leaf์ธ ๊ฒฝ์ฐ, ํด๋น ์์ฒญ์ ์ง์ ์ ์ผ๋ก ์ฒ๋ฆฌ๋๋ค. ๋ง์ผ ์ํธ์์ฉํ๋ ๊ฐ์ฒด๊ฐ Composite์ธ ๊ฒฝ์ฐ, Composite๋ ํด๋น ์์ฒญ์ ์์ ์ปดํฌ๋ํธ๋ค์๊ฒ ์ ๋ฌํ๋๋ฐ, ์์ ์ปดํฌ๋ํธ๋ค์๊ฒ ํด๋น ์์ฒญ์ ์ ๋ฌํ๊ธฐ ์ ๋๋ ํ์ ์ถ๊ฐ์ ์ธ ๋ช
๋ น๋ค์ ์ํํ ์ ์๋ค.
1.7. Consequences ¶
CompositePattern ์
- ๊ธฐ๋ณธ ๊ฐ์ฒด๋ค๊ณผ ๋ณตํฉ ๊ฐ์ฒด๋ค๋ก ๊ตฌ์ฑ๋ ํด๋์ค ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ์ ์ํ๋ค. (์์๊ด๊ณ๊ฐ ์๋. ์ฌ๊ธฐ์๋ ์ผ์ข
์ data-structure์ ๊ด์ ) ๊ธฐ๋ณธ ๊ฐ์ฒด๋ค์ ๋ ๋ณต์กํ ๊ฐ์ฒด๋ค์ ๊ตฌ์ฑํ ์ ์๊ณ , ๊ณ์์ ์ด๊ณ ์ฌ๊ท์ ์ผ๋ก ์กฐํฉ๋ ์ ์๋ค. ํด๋ผ์ด์ธํธ ์ฝ๋๊ฐ ๊ธฐ๋ณธ ๊ฐ์ฒด๋ฅผ ์ํ ๋ ์ด๋์๋ ์ง ๋ณตํฉ ๊ฐ์ฒด๋ฅผ ์ทจํ ์ ์๋ค.
- ํด๋ผ์ด์ธํธ๋ฅผ ๋จ์ํ๊ฒ ๋ง๋ ๋ค. ํด๋ผ์ด์ธํธ๋ ๊ฐ๊ฐ์ ๊ฐ์ฒด์ ๋ณตํฉ ๊ตฌ์กฐ์ฒด๋ฅผ ๋๋ฑํ๊ฒ ์ทจ๊ธํ ์ ์๋ค. ํด๋ผ์ด์ธํธ๋ ๊ทธ๋ค์ด ๋ค๋ฃจ๋ ค๋ ๊ฐ์ฒด๊ฐ Composite ์ธ์ง Leaf ์ธ์ง ์ ํ์๊ฐ ์๋ค. ์ด๋ฌํจ์ composition ์ ์ ์ํ๋ ํด๋์ค๋ค์ ๋ํด ์๊ด์์ด ํจ์๋ค์ ๋จ์ํ๊ฒ ํด์ค๋ค.
- ์๋ก์ด ์ข
๋ฅ์ ์ปดํฌ๋ํธ๋ค์ ์ถ๊ฐํ๊ธฐ ์ฝ๊ฒ ํด์ค๋ค. ์๋ก ์ ์๋ Composite ๋ Leaf ์ ์๋ธํด๋์ค๋ค์ ์๋์ ์ผ๋ก ํ์ฌ์ ๊ตฌ์กฐ๋ค๊ณผ ํด๋ผ์ด์ธํธ ์ฝ๋๋ค๊ณผ ์์ฉํ๋ค. ํด๋ผ์ด์ธํธ ์ฝ๋๋ค์ ์๋ก์ด Component ํด๋์ค๋ค์ ๋ํด์ ์์ ๋ ํ์๊ฐ ์๋ค.
- ๋์์ธ์ ์ข ๋ ์ผ๋ฐํ์์ผ์ค๋ค. ์๋ก์ด ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํ๊ธฐ ์ฝ๋ค๋ ์ ์ด ๋จ์ ์ผ๋ก ์์ฉํ๋ ๊ฒฝ์ฐ๋ composite์ ์ปดํฌํดํธ๋ค๋ก ํ์ฌ๊ธ ์ ํ์ ๋๊ธฐ ํ๋ค๋ค๋ ์ ์ด ์๋ค. ๋๋๋ก ํน์ ์ปดํฌ๋ํธ๋ค๋ง์ ๊ฐ์ง๋ composite๋ฅผ ์ํ ๋๊ฐ ์์ ๊ฒ์ด๋ค. Composite ์ธ ๊ฒฝ์ฐ, ์ด๋ฌํ ์ ํ์ ๊ฐ์ ๋ ๋๊ธฐ ์ํด type system์ ์์กดํ ์ ์๊ฒ ๋๋ค. ๊ทธ ๋์ ์ค์๊ฐ ์ฒดํฌ๋ฅผ ์ด์ฉํด์ผ ํ ๊ฒ์ด๋ค.
1.8. Implementation ¶
CompositePattern์ ๊ตฌํํ ๋ ๊ณ ๋ คํด์ผ ํ ์ฌ๋ฌ๊ฐ์ง ์ฌํญ๋ค์ด ์๋ค.
- ๋ถ๋ชจ์ ์ฐธ์กฐ์๋ฅผ ๋ช
์ํ๋ผ.
- ์ปดํฌ๋ํธ๋ค์ ๊ณต์
- Component ์ธํฐํ์ด์ค์ ์ต๋ํ
- Child ๊ด๋ฆฌ ๋ช
๋ น์ด๋ค์ ์ ์ธ
- Component๋ ์ปดํฌ๋ํธ๋ค์ ๋ฆฌ์คํธ๊ตฌ์กฐ๋ฅผ ๊ตฌํํด์ผ ํ๋๊ฐ?
- Child ordering
- ์ฑ๋ฅํฅ์์ ์ํ caching
- ๋๊ฐ ์ปดํฌ๋ํธ๋ค์ ์ญ์ ํด์ผ ํ๋๊ฐ?
- ์ปดํฌ๋ํธ๋ค์ ์ ์ฅํ๊ธฐ ์ํ ๊ฐ์ฅ ์ข์ ์๋ฃ๊ตฌ์กฐ๋ ๋ฌด์์ธ๊ฐ?
1.9. Sample Code ¶
computer ์ ์คํ
๋ ์ค ์ปดํฌ๋ํธ๋ค๊ณผ ๊ฐ์ ์ฅ์น๋ค (Equipment) ๋ ๋ณดํต ๊ฒฉ๋ฉ ๊ณ์ธต์ ๋ถ๋ถ-์ ์ฒด ์์ผ๋ก ๊ตฌ์ฑ๋๋ค. ์๋ฅผ ๋ค์ด ์์ (chassis) ๋ ๋๋ผ์ด๋ธ๋ค(ํ๋๋์คํฌ ๋๋ผ์ด๋ธ, ํ๋กํผ ๋์คํฌ ๋๋ผ์ด๋ธ ๋ฑ) ๊ณผ ํํ๋ค (์ปดํจํฐ ์ผ์ด์ค์ ๋์ ํ๋ค) ์ ํฌํจํ๊ณ , ๋ฒ์ค๋ ์นด๋๋ค์ ํฌํจํ ์ ์๊ณ , ์บ๋น๋ท์ ์์์ ๋ฒ์ค ๋ฑ๋ฑ์ ํฌํจํ ์ ์๋ค. ์ด๋ฌํ ๊ตฌ์กฐ๋ ์์ฐ์ค๋ฝ๊ฒ CompositePattern์ผ๋ก ๋ชจ๋ธ๋ง๋ ์ ์๋ค.
Equipment ํด๋์ค๋ ๋ถ๋ถ-์ ์ฒด ๊ณ์ธต๊ตฌ์กฐ์ ๋ชจ๋ equipment๋ค์ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๋ค.
~cpp class Equipment { public: virtual ~Equipment (); const char* Name () { return _name; } virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice (); virtual void Add (Equipment* ); virtual void Remove (Equipment* ); virtual Iterator<Equipment*>* CreateIterator (); protected: Equipment (const char*); private: const char* _name; };
Equipment ๋ ์ ์์๋ชจ๋ (power consumption)๊ณผ ๊ฐ๊ฒฉ(cost) ๋ฑ๊ณผ ๊ฐ์ equipment์ ์ผ๋ถ์ ์์ฑ๋ค์ ๋ฆฌํดํ๋ ๋ช
๋ น๋ค์ ์ ์ธํ๋ค. ์๋ธํด๋์ค๋ค์ ํด๋น ์ฅ๋น์ ๊ตฌ์ฒด์ ์ข
๋ฅ์ ๋ฐ๋ผ ์ด ๋ช
๋ น๋ค์ ๊ตฌํํ๋ค. Equipment ๋ ๋ํ Equipment์ ์ผ๋ถ๋ฅผ ์ ๊ทผํ ์ ์๋ Iterator ๋ฅผ ๋ฆฌํดํ๋ CreateIterator ๋ช
๋ น์ ์ ์ธํ๋ค. ์ด ๋ช
๋ น์ ๊ธฐ๋ณธ์ ์ธ ๊ตฌํ๋ถ๋ ๋น์ด์๋ ์งํฉ์ ๋ํ NullIterator ๋ฅผ ๋ฆฌํดํ๋ค.
Equipment ์ ์๋ธํด๋์ค๋ ๋์คํฌ ๋๋ผ์ด๋ธ๋ IC ํ๋ก, ์ค์์น ๋ฑ์ ํํํ๋ Leaf ํด๋์ค๋ฅผ ํฌํจํ ์ ์๋ค.
~cpp class FloppyDisk : public Equipment { public: FloppyDisk (const char*); virtual ~FloppyDisk (); virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice (); };
CompositeEquipment ๋ ๋ค๋ฅธ equipment๋ฅผ ํฌํจํ๋ equipment์ ๊ธฐ๋ณธํด๋์ค์ด๋ค. ์ด๋ ๋ํ Equipment ์ ์๋ธํด๋์ค์ด๋ค.
~cpp class CompositeEquipment : public Equipment { public: virtual ~CompositeEquipment (); virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice (); virtual void Add (Equipement* ); virtual void Remove (Equipment* ); virtual Iterator<Equipment*>* CreateIterator (); protected: CompositeEquipment (const char*); private: List<Equipment*>_equipment; };
CompositeEquipment ๋ sub-equipment ์ ์ ๊ทผํ๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํ ๋ช
๋ น๋ค์ ์ ์ํ๋ค. ์ด ๋ช
๋ น๋ค์ธ Add ์ Remove๋ _equipment ๋ฉค๋ฒ์ ์ ์ฅ๋ equipment ์ ๋ฆฌ์คํธ๋ก๋ถํฐ equipment ๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๋ค. CreateIterator ๋ช
๋ น์ ์ด ๋ฆฌ์คํธ๋ค์ ํ์ํ ์ ์๋ iterator(๊ตฌ์ฒด์ ์ผ๋ก ListIterator์ ์ธ์คํด์ค) ๋ฅผ ๋ฆฌํดํ๋ค.
NetPrice ์ ๊ธฐ๋ณธ ๊ตฌํ๋ถ๋ sub-equipment ์ net price์ ํฉ์ ๊ตฌํ๊ธฐ ์ํด CreateIterator๋ฅผ ์ด์ฉํ ๊ฒ์ด๋ค.
~cpp Currency CompositeEquipment::NetPrice () { Iterator<Equipment*>* i = CreateIterator (); Currency total = 0; for (i->first (); !i->IsDone (); i->Next ()) { total += i->CurrentItem ()->NetPrice (); } delete i; return total; }
์, ์ฐ๋ฆฌ๋ ์ปดํจํฐ ์์๋ฅผ Chassis ๋ผ ๋ถ๋ฆฌ๋ CompositeEquipment์ ์๋ธํด๋์ค๋ก์ ํํํ ์ ์๋ค. Chassis๋ CompositeEquipment๋ก๋ถํฐ ์์-๊ด๋ จ ๋ช
๋ น์ด๋ค์ ์์๋ฐ๋๋ค.
~cpp class Chassis : public CompositeEquipment { public: Chassis (const char*); virtual ~Chassis (); virtual Watt Power (); virtual Currency NetPrice (); virtual Currency DiscountPrice (); };
์ฐ๋ฆฌ๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ผ๋ก Cabinet ๋ Bus ์ ๊ฐ์ ๋ค๋ฅธ equipment ์ปจํ
์ด๋๋ฅผ ์ ์ํ ์ ์๋ค. ์ด๋ก์ ์ฐ๋ฆฌ๊ฐ ๊ฐ์ธ์ฉ ์ปดํจํฐ์ equipment๋ค์ ์กฐ๋ฆฝํ๊ธฐ ์ํด (๊ฝค ๊ฐ๋จํ๊ฒ) ํ์๋ก ํ๋ ๋ชจ๋ ๊ฒ๋ค์ด ์ฃผ์ด์ก๋ค.
~cpp Cabinet* cabinet = new Cabinet ("PC Cabinet"); Chassis* chassis = new Chassis ("PC Chassis"); cabinet->Add (chassis); Bus* bus = new Bus ("MCA Bus"); bus->Add (new Card("16Mbs Token Ring")); chassis->Add (bus); chassis->Add (new FloppyDisk ("3.5in Floppy")); cout << "The net price is " << chassis->NetPrice () << endl;
1.10. Known Uses ¶
CompositePattern์ ์๋ ๊ฑฐ์ ๋ชจ๋ ๊ฐ์ฒด์งํฅ ์์คํ
์์ ์ฐพ์ ์ ์๋ค. Smalltalk ์ Model/View/Container KP88 ์ original View ํด๋์ค๋ Composite์ด๋ฉฐ, ET++ (VObjects WGM88) ์ด๋ InterViews (Styles LCI+92, Graphics VL88, Glyphs CL90)๋ฑ ๊ฑฐ์ ๋๋ถ๋ถ์ ์ ์ ์ธํฐํ์ด์ค ํดํท๊ณผ ํ๋ ์์ํฌ๊ฐ ํด๋น ๊ณผ์ ์ ๋ฐ๋ฅธ๋ค. Model/View/Controller ์ original View์์ ์ฃผ๋ชฉํ ๋งํ ์ ์ subview ์ ์งํฉ์ ๊ฐ์ง๋ค๋ ๊ฒ์ด๋ค. ๋ค์ ๋งํ๋ฉด, View๋ Component class ์ด์ Composite class ์ด๋ค. Smalltalk-80 ์ Release 4.0 ์ View ์ CompositeView ์ ์๋ธํด๋์ค๋ฅผ ๊ฐ์ง๋ VisualComponent ํด๋์ค๋ก Model/View/Controller ๋ฅผ ๋ณ๊ฒฝํ๋ค.
RTL Smalltalk ์ปดํ์ผ๋ฌ ํ๋ ์์ํฌ JML92 ๋ CompositePattern์ ๋๋ฆฌ ์ฌ์ฉํ๋ค. RTLExpression ์ parse tree๋ฅผ ์ํ Component ํด๋์ค์ด๋ค. RTLExpression ์ BinaryExpression ๊ณผ ๊ฐ์ ์๋ธํด๋์ค๋ฅผ ๊ฐ์ง๋๋ฐ, ์ด๋ RTLExpression ๊ฐ์ฒด๋ค์ ์์์ผ๋ก ํฌํจํ๋ค. ์ด ํด๋์ค๋ค์ parse tree๋ฅผ ์ํด composite ๊ตฌ์กฐ๋ฅผ ์ ์ํ๋ค. RegisterTransfer ๋ ํ๋ก๊ทธ๋จ์ Single Static Assignment(SSA) ํํ์ ์ค๊ฐ๋ฌผ์ ์ํ Component ํด๋์ค์ด๋ค. RegisterTransfer ์ Leaf ์๋ธํด๋์ค๋ค์ ๋ค์๊ณผ ๊ฐ์ ๋ค๋ฅธ ํํ์ static assignment ๋ฅผ ์ ์ํ๋ค.
๋ ๋ค๋ฅธ ์๋ธํด๋์ค๋ก์ RegisterTransferSet์ด ์๋ค. RegisterTransferSet ๋ ํ๋ฒ์ ์ฌ๋ฌ register๋ฅผ ๋ณ๊ฒฝํ๋ assignment๋ฅผ ํํํ๊ธฐ ์ํ Composite ํด๋์ค์ด๋ค.
- ๋๊ฐ์ ๋ ์ง์คํฐ๋ก ๋ช
๋ น์ ์ํํ๊ณ ์ธ๋ฒ์งธ ๋ ์ง์คํฐ๋ก ๊ฒฐ๊ณผ๊ฐ์ ํ ๋นํ๋ ๊ธฐ๋ณธ์ ์ธ assignment
- source register ๋ฅผ ๊ฐ์ง์ง๋ง, destination register ๋ฅผ ๊ฐ์ง์ง ์๋, register๊ฐ ํด๋น ๋ฃจํด์ด ๋ฆฌํด ๋ ๋ค์ ์ด์ฉ๋๋ assignment
- destination register ๋ฅผ ๊ฐ์ง์ง๋ง, source register ๋ฅผ ๊ฐ์ง์ง ์๋, ํด๋น ๋ฃจํด์ด ์์๋๊ธฐ ์ ์ register ๊ฐ assign๋๋ assignment
๋ ๋ค๋ฅธ ์๋ธํด๋์ค๋ก์ RegisterTransferSet์ด ์๋ค. RegisterTransferSet ๋ ํ๋ฒ์ ์ฌ๋ฌ register๋ฅผ ๋ณ๊ฒฝํ๋ assignment๋ฅผ ํํํ๊ธฐ ์ํ Composite ํด๋์ค์ด๋ค.
Another example of this pattern occurs in the financial domain, where a portfolio aggregates individual assets. You can support complex aggregations of assets by implementing a portfolio as a Composite that conforms to the interface of an individual asset BE93.
CompositePattern์ ๋๋ค๋ฅธ ์๋ ๊ฐ๊ฐ์ ์์ฐ๋ค์ ํฌํจํ๋ portfolio์ธ financial domain ์์ ๋ํ๋๋ค. portfolio ๋ฅผ ๊ฐ๊ฐ์ asset ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌ์ฑํ๋ Composite ๋ก ๊ตฌํํจ์ผ๋ก์จ ๋ณต์กํ asset์ ํฌํจ๊ด๊ณ๋ฅผ ์ง์ํ ์ ์๋ค.
CommandPattern์ MacroCommand Composite ํด๋์ค๋ก Command ๊ฐ์ฒด๋ค์ด ์กฐํฉ๋๊ณ ๋์ดํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์ ํ๋ค.
1.11. Related Patterns ¶
- ์ข
์ข
์ปดํฌ๋ํธ-๋ถ๋ชจ ์ฐ๊ฒฐ์ ChainOfResponsibilityPattern์ ์ด์ฉ๋๋ค.
- DecoratorPattern ์ ์ข
์ข
Composite์ ํจ๊ป ์ด์ฉ๋๋ค. descorator ์ composite ๊ฐ ํจ๊ป ์ด์ฉ๋ ๋, ๊ทธ๊ฒ๋ค์ ๋ณดํต ๊ณตํต๋ ๋ถ๋ชจ ํด๋์ค๋ฅผ ๊ฐ์ง ๊ฒ์ด๋ค. ๊ทธ๋ฌํ ๊ฒฝ์ฐ decorator๋ Add, Remove, GetChild ์ ๊ฐ์ Compoent ์ ์ธํฐํ์ด์ค๋ฅผ ์ง์ํด์ผ ํ๋ค.
- FlyweightPattern lets you share components, but they can no longer refer to their parents.
- FlyweightPattern ์ ์ปดํฌ๋ํธ๋ค์ ๊ณต์ ํ ์ ์๋๋ก ํด์ฃผ์ง๋ง, ๊ทธ๋ค์ ๋ถ๋ชจ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
- IteratorPattern ์ composite๋ค์ ํ์ํ ๋ ์ด์ฉ๋ ์ ์๋ค.
- VisitorPattern์ ๋ช
๋ น๋ค๊ณผ Composite ์ Leaf ํด๋์ค ์ฌ์ด๋ฅผ ๊ฐ๋ก์ง๋ฌ ๋ถํฌ๋ ์ ์๋ ํ์๋ค์ ์ง์ญํํ๋ค.