์ปดํฌ์ง€ํŠธ ํŒจํ„ด(Composite Pattern)

2023. 5. 29. 14:21ยท๐Ÿ“ Computer Science/โœ Design Pattern

์ปดํฌ์ง€ํŠธ ํŒจํ„ด(Composite Pattern)

์ปดํฌ์ง€ํŠธ

OOP์—์„œ ์ปดํฌ์ง€ํŠธ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ์œ ์‚ฌํ•œ ๊ฐ์ฒด๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ ๊ฐ์ฒด๋กœ ๋ชจ๋‘ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ๊ทธ๋ฃน์„ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋‹จ์ผ ๊ฐ์ฒด๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ปดํฌ์ง€ํŠธ ํŒจํ„ด

์ปดํฌ์ง€ํŠธ ํŒจํ„ด์€ ๋ณตํ•ฉ ๊ฐ์ฒด๋‚˜ ๋‹จ์ผ ๊ฐ์ฒด๋ฅผ ๋™์ผํ•˜๊ฒŒ ์ทจ๊ธ‰ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉ์ ์œผ๋กœ ํ•œ๋‹ค. ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ์ž‘์„ฑํ•˜์—ฌ ์ „์ฒด-๋ถ€๋ถ„(whole-part) ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค.

Composite Pattern

 

1. ๊ตฌํ˜„

์‚ฌ์šฉ์ž๋Š” Leaf์™€ Composite ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ , ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค Component๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

1) Component

  • ๋ชจ๋“  Component๋“ค์„ ์œ„ํ•œ ์ถ”์ƒํ™”๋œ ๊ฐœ๋…์ด๋‹ค.
  • Leaf์™€ Composite ํด๋ž˜์Šค์˜ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.
interface Graphic {
    public void print();
}

 

2) Leaf

  • Component ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ๊ตฌ์ฒด ํด๋ž˜์Šค๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
class Ellipse implements Graphic {
    public void print() {
        System.out.println("Ellipse");
    }
}

 

3) Composite

  • Component ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ๊ตฌํ˜„๋˜๋Š” ์ž์‹๋“ค์„ ๊ฐ€์ง€๊ณ  ์ด๋Ÿฌํ•œ ์ž์‹๋“ค์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ(add, remove ๋“ฑ)๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ ์ธํ„ฐํŽ˜์ด์Šค์— ์ž‘์„ฑ๋œ ๋ฉ”์„œ๋“œ๋Š” ์ž์‹์—๊ฒŒ ์œ„์ž„ํ•˜๋Š” ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
class CompositeGraphic implements Graphic {
    // Collection of child graphics.
    private List<Graphic> childGraphics = new ArrayList<Graphic>();

    public void print() {
        for (Graphic graphic : childGraphics) {
            graphic.print();
        }
    }
    
    public void add(Graphic graphic) {
        childGraphics.add(graphic);
    }
    
    public void remove(Graphic graphic) {
        childGraphics.remove(graphic);
    }
}

 

4) Client

ํŠธ๋ฆฌ ๊ตฌ์กฐ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉด์„œ print()์™€ ๊ฐ™์ด ๋‹จ์ผ ๊ฐ์ฒด์™€ ๋ณตํ•ฉ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ํ˜•ํƒœ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค.

public class Program {

    public static void main(String[] args) {
        //Initialize four ellipses
        Ellipse ellipse1 = new Ellipse();
        Ellipse ellipse2 = new Ellipse();
        Ellipse ellipse3 = new Ellipse();
        Ellipse ellipse4 = new Ellipse();

        //Initialize three composite graphics
        CompositeGraphic graphic = new CompositeGraphic();
        CompositeGraphic graphic1 = new CompositeGraphic();
        CompositeGraphic graphic2 = new CompositeGraphic();

        //Composes the graphics
        graphic1.add(ellipse1); // children - leaf
        graphic1.add(ellipse2); // children - leaf
        graphic1.add(ellipse3); // children - leaf

        graphic2.add(ellipse4); // children - leaf

        graphic.add(graphic1); // children - composite
        graphic.add(graphic2); // children - composite

        //Prints the complete graphic (Four times the string "Ellipse").
        graphic.print();
    }
}

 

2. ์‚ฌ์šฉ

Client์—์„œ ํŠธ๋ฆฌ ๊ตฌ์กฐ์—์„œ์˜ top-level์— ์กด์žฌํ•˜๋Š” Composite1์— ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด Component ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด๋“ค์€ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ํ† ๋Œ€๋กœ ์œ„์—์„œ ์•„๋ž˜ ๋ฐฉํ–ฅ์œผ๋กœ ๋ชจ๋“  ์ž์‹ ์š”์†Œ์—๊ฒŒ ์ „๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๊ฒƒ์€ ์‹ค์ œ๋กœ ๋Ÿฐํƒ€์ž„์—์„œ ์ผ์–ด๋‚˜๋Š” ํ–‰์œ„๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

 

๊ฐ์ฒด ๋‹ค์ด์–ด๊ทธ๋žจ

 

3. ์ฐธ๊ณ 

https://mygumi.tistory.com/343

 

์ปดํฌ์ง€ํŠธ ํŒจํ„ด(Composite Pattern) :: ๋งˆ์ด๊ตฌ๋ฏธ

์ด ๊ธ€์€ ๋””์ž์ธ ํŒจํ„ด ์ค‘ ์ปดํฌ์ง€ํŠธ ํŒจํ„ด(Composite Pattern) ์„ ๋‹ค๋ฃฌ๋‹ค.์œ„ํ‚คํ”ผ๋””์•„์˜ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •๋ฆฌํ•  ์˜ˆ์ •์ด๋‹ค.์œ„ํ‚ค - https://en.wikipedia.org/wiki/Composite_pattern ๊ธ€์˜ ์ฃผ์ œ๋ฅผ ๋‹ค๋ฃจ๊ธฐ์— ์•ž์„œ, ๊ธ€๋“ค์„

mygumi.tistory.com

 

์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Design Pattern' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • ์ŠคํŠธ๋ž˜ํ‹ฐ์ง€ ํŒจํ„ด(Strategy Pattern)
  • [๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด] ๊ฒŒ์ž„ ๊ฐœ๋ฐœ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋””์ž์ธ ํŒจํ„ด
  • [๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด] ๋”ํ‹ฐ ํ”Œ๋ž˜๊ทธ ํŒจํ„ด(Dirty Flag Pattern)
  • [๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจํ„ด] ๊ฒŒ์ž„ ๋ฃจํ”„ ํŒจํ„ด(Game Loop Pattern)
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
์ปดํฌ์ง€ํŠธ ํŒจํ„ด(Composite Pattern)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”