Post

Adapter Pattern

Adapter Pattern

개요

  • 기존 클래스의 인터페이스를 변경하지 않고도 다른 인터페이스를 제공하는 패턴.

구성요소

  • Target: 클라이언트가 사용하고자 하는 인터페이스
  • Adapter: Target 인터페이스를 구현하고, Adaptee의 인스턴스를 포함하여 Adaptee의 기능을 Target 인터페이스로 변환하는 클래스
  • Adaptee: 기존 클래스의 인터페이스
  • Client: Target 인터페이스를 사용하는 클라이언트

예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <memory> // 스마트 포인터 사용
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
// Target 인터페이스
class Target {
public:
    virtual void request() = 0;
    virtual ~Target() = default; // 가상 소멸자 추가 (메모리 릭 방지)
};

// Adaptee 클래스
class Adaptee {
public:
    void specificRequest() {
        cout << "Adaptee::specificRequest" << endl;
    }
};

// Adapter 클래스
class Adapter : public Target {
private:
    unique_ptr<Adaptee> adaptee; // 스마트 포인터 사용
public:

    Adapter(unique_ptr<Adaptee> adaptee) : adaptee(move(adaptee)) {}

    void request() override {
        cout << "Adapter::request" << endl;
        adaptee->specificRequest();
    }
};

// Client 클래스
class Client {
public:
    void useAdapter(unique_ptr<Target> target) {
        target->request();
    }
};

int main() {
    unique_ptr<Adaptee> adaptee = make_unique<Adaptee>();
    unique_ptr<Target> adapter = make_unique<Adapter>(move(adaptee));
    
    Client client;
    client.useAdapter(move(adapter));

    return 0;
}
This post is licensed under CC BY 4.0 by the author.