U E D R , A S I H C RSS

Gof/Composite

1. Composite

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 κ°μ²΄κ΅¬μ‘°λŠ” λ‹€μŒκ³Ό 같은 λͺ¨μŠ΅μ„ λˆλ‹€.

1.5. Participants

  • Component (Graphic)
    • composition μ—μ˜ 객체듀에 λŒ€ν•œ μΈν„°νŽ˜μ΄μŠ€λΌ μ„ μ–Έν•œλ‹€.
    • λͺ¨λ“  ν΄λž˜μŠ€λ“€μ—μ˜ 기본적인 μΈν„°νŽ˜μ΄μŠ€μ— λŒ€ν•œ κΈ°λ³Έ ν–‰μœ„λΌ κ΅¬ν˜„ν•œλ‹€.
    • μžμ‹ μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ ‘κ·Όν•˜κ³  κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€λΌ μ„ μ–Έν•œλ‹€.
    • (optional) μž¬κ·€ κ΅¬μ‘°μ—μ„œμ˜ ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈμ˜ λΆ€λͺ¨ 클래슀둜 μ ‘κ·Όν•˜κΈ° μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€λΌ μ •μ˜ν•˜κ³ , μ μ ˆν•˜κ²Œ κ΅¬ν˜„ν•œλ‹€.
  • Leaf (Rectangle, Line, Text, etc.)
    • composition의 leaf κ°μ²΄λΌ ν‘œν˜„ν•œλ‹€. leaf λŠ” childrenλΌ κ°€μ§€μ§€ μ•ŠλŠ”λ‹€.
    • composition 의 κΈ°λ³Έ κ°μ²΄λ“€μ˜ ν–‰μœ„λΌ μ •μ˜ν•œλ‹€.
  • Composite (Picture)
    • children을 κ°€μ§€λŠ” μ»΄ν¬λ„ŒνŠΈλ“€μ„ μœ„ν•œ ν–‰μœ„λΌ μ •μ˜ν•œλ‹€.
    • μžμ‹ μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ €μž₯ν•œλ‹€.
    • Component μΈν„°νŽ˜μ΄μŠ€μ€‘ μžμ‹μ»΄ν¬λ„ŒνŠΈ κ΄€λ ¨ λͺ…령듀을 κ΅¬ν˜„ν•œλ‹€.
  • Client
    • 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을 κ΅¬ν˜„ν•  λ•Œ κ³ λ €ν•΄μ•Ό ν•  μ—¬λŸ¬κ°€μ§€ 사항듀이 μžˆλ‹€.
  1. λΆ€λͺ¨μ˜ μ°Έμ‘°μžλΌ λͺ…μ‹œν•˜λΌ.
  2. μ»΄ν¬λ„ŒνŠΈλ“€μ˜ 곡유
  3. Component μΈν„°νŽ˜μ΄μŠ€μ˜ μ΅œλŒ€ν™”
  4. Child 관리 λͺ…λ Ήμ–΄λ“€μ˜ μ„ μ–Έ
  5. ComponentλŠ” μ»΄ν¬λ„ŒνŠΈλ“€μ˜ λ¦¬μŠ€νŠΈκ΅¬μ‘°λΌ κ΅¬ν˜„ν•΄μ•Ό ν•˜λŠ”κ°€?
  6. Child ordering
  7. μ„±λŠ₯ν–₯상을 μœ„ν•œ caching
  8. λˆ„κ°€ μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ‚­μ œν•΄μ•Ό ν•˜λŠ”κ°€?
  9. μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ €μž₯ν•˜κΈ° μœ„ν•œ κ°€μž₯ 쒋은 μžλ£Œκ΅¬μ‘°λŠ” 무엇인가?

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 λΌ μ •μ˜ν•œλ‹€.
  • λ‘κ°œμ˜ λ ˆμ§€μŠ€ν„°λ‘œ λͺ…령을 μˆ˜ν–‰ν•˜κ³  μ„Έλ²ˆμ§Έ λ ˆμ§€μŠ€ν„°λ‘œ 결과값을 ν• λ‹Ήν•˜λŠ” 기본적인 assignment
  • source register λΌ κ°€μ§€μ§€λ§Œ, destination register λΌ κ°€μ§€μ§€ μ•ŠλŠ”, registerκ°€ ν•΄λ‹Ή 루틴이 리턴 된 뒀에 μ΄μš©λ˜λŠ” assignment
  • destination register λΌ κ°€μ§€μ§€λ§Œ, source register λΌ κ°€μ§€μ§€ μ•ŠλŠ”, ν•΄λ‹Ή 루틴이 μ‹œμž‘λ˜κΈ° 전에 register κ°€ assignλ˜λŠ” assignment

Another subclass, RegisterTransferSet, is a Composite class for representing assignments that change several registers at once.
또 λ‹€λ₯Έ μ„œλΈŒν΄λž˜μŠ€λ‘œμ„œ 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 클래슀 μ‚¬μ΄λΌ κ°€λ‘œμ§ˆλŸ¬ 뢄포될 수 μžˆλŠ” ν–‰μœ„λ“€μ„ μ§€μ—­ν™”ν•œλ‹€.


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:19
Processing time 0.0564 sec