# 프록시 패턴

---

프록시 개체는 클라이언트와 실제 객체 간의 중개자 역할을 하며 캐싱, 액세스 제어 및 로깅과 같은 추가 기능을 제공하는 데 사용할 수 있다.

다음은 fetch된 데이터를 캐싱하는 예제이다.

```
class ApiService {
  async fetchData(id) {
    // 여기에서 실제 API 호출을 수행합니다.
    console.log(`Fetching data for ID: ${id}`);
    return `Data for ID: ${id}`;
  }
}

class ApiProxy {
  constructor(apiService) {
    this.apiService = apiService;
    this.cache = new Map();
  }

  async fetchData(id) {
    if (this.cache.has(id)) {
      console.log(`Returning cached data for ID: ${id}`);
      return this.cache.get(id);
    }

    const data = await this.apiService.fetchData(id);
    this.cache.set(id, data);
    return data;
  }
}

const apiService = new ApiService();
const apiProxy = new ApiProxy(apiService);

(async () => {
  console.log(await apiProxy.fetchData(1)); // API 호출을 수행하고 캐싱합니다.
  console.log(await apiProxy.fetchData(1)); // 캐시된 데이터를 반환합니다.
})();
```

For the site tree, see the [root Markdown](https://slashpage.com/develop.md).
