> For the complete documentation index, see [llms.txt](https://fistkim101.gitbook.io/tech-blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fistkim101.gitbook.io/tech-blog/undefined/gof/undefined-1/undefined-2.md).

# 컴포짓 패턴

## 대분류

구조

## 문제상황

클라이언트가 사용하고자 하는 타겟 오브젝트의 위계를 전혀 신경 쓰지 않고 통일된 어떠한 기능을 수행하고자 한다. 부분이든 전체든 동일한 기능을 수행해준다.

## 해결방안

위계에 속한 모든 컴포넌트들이 동일한 인터페이스를 구현하도록 하고, 클라이언트에서는 이 인터페이스를 통해서 메세지를 전달한다.

<figure><img src="/files/MIs6Gke5ZhtaHGpo2Vf8" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/eqpH7mIlBBkmspOSZDER" alt=""><figcaption></figcaption></figure>

## 실습코드

```java
public interface Employee {
    void work();
}

public class Junior implements Employee{
    @Override
    public void work() {
        System.out.println("Junior is working");
    }
}

public class Team implements Employee {
    private final List<Employee> employees;

    public Team(List<Employee> employees) {
        this.employees = employees;
    }

    @Override
    public void work() {
        System.out.println("team is working start ---");
        this.employees.forEach(Employee::work);
        System.out.println("team is working end ---");
    }
}

public class Client {
    public static void main(String[] args) {
        Employee junior1 = new Junior();
        junior1.work();
        Employee junior2 = new Junior();
        junior2.work();

        Employee team = new Team(List.of(junior1, junior2));
        team.work();
    }
}
```

사실 이건 [일급컬렉션](https://jojoldu.tistory.com/412)과 크게 다르지 않다. 일급컬렉션 개념에 통일된 인터페이스의 구현을 강제한 것이 컴포짓 패턴이다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fistkim101.gitbook.io/tech-blog/undefined/gof/undefined-1/undefined-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
