-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path01 - traditional implementation.cpp
More file actions
48 lines (39 loc) · 1.1 KB
/
01 - traditional implementation.cpp
File metadata and controls
48 lines (39 loc) · 1.1 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
#include <string>
#include <iostream>
using namespace std::literals;
struct Connection {
virtual ~Connection() = default;
virtual void send(const char*) = 0;
};
struct TCPConnection : public Connection {
void send(const char *s) override { std::cout << "TCP: " << s << '\n'; }
};
struct UDPConnection : public Connection {
void send(const char *s) override { std::cout << "UDP: " << s << '\n'; }
};
struct ConnectionFactory {
virtual ~ConnectionFactory() = default;
virtual Connection *make() = 0;
};
struct TCPConnectionFactory : public ConnectionFactory {
Connection *make() override { return new TCPConnection(); }
};
struct UDPConnectionFactory : public ConnectionFactory {
Connection *make() override { return new UDPConnection(); }
};
void send(ConnectionFactory *conFactory) {
Connection *con = conFactory->make();
con->send("Hello");
delete con;
}
int main(int argc, char *argv[]) {
ConnectionFactory *conFactory;
if (argc > 1 && argv[1] == "tcp"s) {
conFactory = new TCPConnectionFactory();
}
else {
conFactory = new UDPConnectionFactory();
}
send(conFactory);
delete conFactory;
}