# 어댑터 패턴

---

어댑터 패턴은 주로 호환되지 않는 두 개의 인터페이스를 함께 사용할 수 있도록 하기 위해 사용된다.

데코레이터 패턴과의 차이는, 데코레이터 패턴은 인터페이스를 바꾸지 않고 기능만 추가하는 반면, 어댑터 패턴은 인터페이스를 변경해서 호환성을 보장하기 위해 사용된다.

다음 예제를 보면 이해에 도움이 된다.

```
class NewAPI {
  request(url) {
    return fetch(url);
  }
}

class OldAPI {
  sendRequest(url, callback) {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.onload = function() {
      callback(xhr.responseText);
    };
    xhr.send();
  }
}

class APIAdapter {
  constructor(oldAPI) {
    this.oldAPI = oldAPI;
  }

  request(url) {
    return new Promise((resolve, reject) => {
      this.oldAPI.sendRequest(url, (response) => {
        resolve(response);
      });
    });
  }
}

// Usage:
const newAPI = new NewAPI();
const oldAPI = new OldAPI();
const adapter = new APIAdapter(oldAPI);
adapter.request('<https://example.com>')
  .then(response => console.log(response));
```

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