Work & Programming/System Design

[Structural Pattern] - Composite

자전거통학 2024. 5. 15. 10:25

https://refactoring.guru/design-patterns/composite

 

Composite

/ Design Patterns / Structural Patterns Composite Also known as: Object Tree Intent Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects. Problem

refactoring.guru

 

어떤 특정 객체가 같은 객체를 재귀적으로 목록으로 가질 수 있는 경우를 표현할 때. 

예시 

 - Dialog System : 하나의 dialog 안에 내부적으로 다른 dialog를 품을 수 있다.

 

 

코드 예시

더보기
public class Window
{
    public void Draw() { }
}

public class Dialog
{
    List<Window> windows = new List<Window>();

    public void AddWindow(Window _win) { }
    public void RemoveWindow(Window _win) { }

    public void Draw()
    {
        for(int q= 0; q < windows.Count; q++)
            windows[q].Draw();
    }
}



internal class ComponentDemo
{
    public void Run()
    {
		Dialog dlg = new Dialog();
        dlg.Draw();
    }
}

'Work & Programming > System Design' 카테고리의 다른 글

[Structural Pattern] - Facade  (0) 2024.05.15
[Structural Pattern] - Decorator  (0) 2024.05.15
[Structural Pattern] - Adapter  (0) 2024.05.15
[Creational Pattern] - Singleton  (0) 2024.05.15
[Creational Pattern] - Builder  (0) 2024.05.15