-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd_cmd.cpp
126 lines (95 loc) · 1.66 KB
/
d_cmd.cpp
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
// cmd pattern decouples the commands and the underlying object
// you just need to pass the underlying concrete object on which you want to call the command
// to the command concrete object
//
#include<iostream>
using namespace std;
//#define TV_EXAMPLE
#define STOCK_ORDER
#ifdef TV_EXAMPLE
class cmd{
public:
virtual void execute()=0;
};
class device{
public:
virtual void on() = 0;
virtual void off() = 0;
//virtual void volincrease() = 0;
//virtual void voldcrease() = 0;
};
class tv:public device{
public:
void on()
{
cout<<"TV on";
}
void off()
{
cout<<"TV off";
}
};
class on:public cmd{
public:
on(device *_d):d(_d){}
device *d;
void execute()
{
d->on();
}
};
class off:public cmd{
public:
off(device *_d):d(_d){}
device *d;
void execute()
{
d->off();
}
};
#endif
#ifdef STOCK_ORDER
class cmd{
public:
virtual void execute()=0;
};
class stockInstrument{
public:
virtual void buy()= 0;
virtual void sell() = 0;
};
class equity:public stockInstrument{
public:
void buy(){ cout<<"buy order";}
void sell() { cout<<"sell order";}
};
class buyorder:public cmd{
public:
buyorder(stockInstrument *_pin):pin(_pin){}
stockInstrument *pin;
void execute()
{
this->pin->buy();
}
};
class sellorder:public cmd{
public:
sellorder(stockInstrument *_pin):pin(_pin){}
stockInstrument *pin;
void execute()
{
this->pin->buy();
}
};
#endif
int main()
{
#ifdef TV_EXAMPLE
cmd *on = new on(new tv());
tvon->execute();
#endif
#ifdef STOCK_ORDER
cmd *buy = new buyorder(new equity);
buy->execute();
#endif
}