-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter_multiple_inheritance.cpp
More file actions
77 lines (61 loc) · 1.55 KB
/
adapter_multiple_inheritance.cpp
File metadata and controls
77 lines (61 loc) · 1.55 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
71
72
73
74
75
76
77
/*
Adapter design pattern is an structural pattern. It make different incompatible objects to cooprate.
One object -> Adapter -> Date Format/Interface can be supported by another object.
For example, XML -> XML/JSON Adapter -> JSON analyzer.
This demo example is implemented by multiple inheritance.
*/
#include <iostream>
#include <algorithm>
using namespace std;
// Defines interfaces used by client code
class client
{
public:
virtual ~client() = default;
// client code does not JSON analyzer code, just only know that JSON format is needed
virtual string xmlAnalysisRequest() const
{
return "I only can provide XML files.";
}
};
// target object - Json format file is needed
class target
{
public:
string jsonAnalyzerRequest() const
{
// xml request might not understand this
return "tupni eb dluohs elif nosJ";
}
};
class adapter : public client, public target
{
public:
adapter() {}
string xmlAnalysisRequest() const override
{
string str = jsonAnalyzerRequest();
reverse(str.begin(), str.end());
return "After adpter, client XML can be analyzed. Client know what Json analyzer told is: " + str;
}
};
void client_code(const client *clnt)
{
cout << clnt->xmlAnalysisRequest();
};
int main()
{
client *clt = new client;
client_code(clt);
cout << "\n";
target *tgt = new target;
cout << tgt->jsonAnalyzerRequest();
cout << "\n";
adapter *adpt = new adapter;
client_code(adpt);
cout << "\n";
delete clt;
delete tgt;
delete adpt;
return 0;
}