-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdapterSample.hpp
126 lines (88 loc) · 2.26 KB
/
AdapterSample.hpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//+++ Êëàññû ñîçäàííûå êåì-òî, èçìåíÿòü íåëüçÿ
class Amper {
public:
Amper( int value ) : value(value) {}
String Log() const { return Format("%d A", value); }
private:
int value;
};
class Volt {
public:
Volt( int value ) : value(value) {}
String Log() const { return Format("%d V", value); }
private:
int value;
};
class Watt {
public:
Watt( int value ) : value(value) {}
String Log() const { return Format("%d W", value); }
private:
int value;
};
class Ampermetr {
public:
Ampermetr( int max ) : max(max) {}
Amper operator()() const { return Amper(Random(max)); }
private:
int max;
};
class Voltmetr {
public:
Voltmetr( int max ) : max(max) {}
Volt operator()() const { return Volt(Random(max)); }
private:
int max;
};
class WattLogger {
public:
void Log(const Watt& w) const { Cout() << w.Log() << EOL; }
};
//--- Êëàññû ñîçäàííûå êåì-òî, èçìåíÿòü íåëüçÿ
//+++ Àäàïòåðû è ïðîêñè
class Adapter {
public:
Adapter(const String& s) { value = StrInt(s); }
Adapter(const Volt& volt) { value = StrInt(volt.Log());}
Adapter(const Amper& amper) { value = StrInt(amper.Log());}
int operator()() const { return value; }
private:
int value;
};
class ProxyWattLogger : public WattLogger {
public:
ProxyWattLogger(Ampermetr& a, Voltmetr& v)
: a(a), v(v) {}
void Log(const Watt& w) const {
int adapav = Adapter(a())();
int adapvv = Adapter(v())();
Cout() << Amper(adapav).Log() << '\t';
Cout() << Volt(adapvv).Log() << '\t';
/*int av = Adapter(a().Log())();
int vv = Adapter(v().Log())();*/
/*Cout() << Amper(av).Log() << '\t';
Cout() << Volt(vv).Log() << '\t';*/
WattLogger::Log(Watt(adapvv * adapav));
}
private:
Ampermetr& a;
Voltmetr& v;
};
//--- Àäàïòåðû è ïðîêñè
// Îñíîâíàÿ ïðîãà
static const int MaxVolt = 220;
static const int MaxAmper = 10;
static const int MaxWatt = MaxVolt * MaxAmper;
void doMain()
{
Ampermetr a(10);
Voltmetr v(220);
WattLogger logger;
// Êàê âûäàåò ïîêàçàíèÿ ïî ñòàðîìó
for (int i = 0; i < 10; i++) {
logger.Log(Watt(Random(MaxWatt)));}
// Íåîáõîäèìî âûâåñòè â âèäå: XXX A; XXX V; XXX W
for (int i = 0; i < 10; i++) {
ProxyWattLogger(a, v).Log(Watt(0));
}
}