-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
73 lines (64 loc) · 1.52 KB
/
command.cpp
File metadata and controls
73 lines (64 loc) · 1.52 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
// Command Design Pattern
#include <iostream>
#include <string>
#include <queue>
// Resource: https://student.cs.uwaterloo.ca/~cs446/1171/Arch_Design_Activity/Command.pdf
// Receives commands/orders
class Chef {
public:
void cook() {
std::cout << "Cooking food!\n";
}
void pourDrink() {
std::cout << "Preparing drink!\n";
}
};
class Order {
public:
virtual void execute() = 0;
protected:
Chef menu;
std::string item;
};
class OrderFood : public Order {
public:
void execute() override {
menu.cook();
}
};
class OrderDrink : public Order {
public:
void execute() override {
menu.pourDrink();
}
};
// Invokes Commands / Orders
class Waiter {
public:
void takeOrder(Order* order) {
orders.push(order);
}
void placeOrders() {
while(!orders.empty()) {
Order* curr = orders.front();
orders.pop();
curr->execute();
}
}
private:
std::queue<Order*> orders;
};
int main() {
Waiter service;
// Create OrderFood and OrderDrink objects - they are directly connected to the chef's "menu"
Order* order1 = new OrderFood();
Order* order2 = new OrderDrink();
// Waiter takes the orders
service.takeOrder(order1);
service.takeOrder(order2);
// Waiter places the orders
service.placeOrders();
delete order1;
delete order2;
return 0;
}