-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectAdapter.cpp
More file actions
70 lines (61 loc) · 1.21 KB
/
ObjectAdapter.cpp
File metadata and controls
70 lines (61 loc) · 1.21 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by osher on 13/02/2022.
//
#include <iostream>
using namespace std;
class Printer {
public:
virtual void print()=0;
};
class DogVoicePrinter : public Printer {
public:
void print() override {
cout << "bark bark.." << endl;
}
};
class CatVoicePrinter : public Printer {
public:
void print() override {
cout << "meow meow.." << endl;
}
};
class Target {
public:
virtual void request()=0;
};
class Adapter :public Target {
Printer* _printer;
public:
Adapter(Printer* printer) {
_printer = printer;
}
void request() override {
_printer->print();
}
~Adapter() {
delete _printer;
}
};
int main() {
char choose;
bool x;
Target* target;
do {
cout << "cat or dog? 'c' / 'd'";
cin >> choose;
switch (choose) {
case 'c':
target = new Adapter(new CatVoicePrinter());
x = false;
break;
case 'd':
target = new Adapter(new DogVoicePrinter());
x = false;
break;
default:
x = true;
}
} while (x);
target->request();
delete target;
}