-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.php
More file actions
executable file
·85 lines (64 loc) · 1.69 KB
/
command.php
File metadata and controls
executable file
·85 lines (64 loc) · 1.69 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
78
79
80
81
82
83
84
85
<?php
/*
COMMAND PATTERN
An object that contains a symbol, name or key that represents a list of commands, actions or keystrokes
The Command Pattern allows you to decouple the requester of an action from the object that actually performs the action.
great for taking inputs and outputs, and not worrying about the details in between
typically used as a good example of queueing as well
pattern :
client
concrete Command
command
receiver
*/
interface Order {
public function execute_order();
}
class StockTrade {
private $stockName;
public function __construct($stockName="") {
$this->stockName = $stockName;
}
public function buy() {
print "You want to buy {$this->stockName}\n";
}
public function sell() {
print "You want to sell {$this->stockName}\n";
}
}
class Agent {
private $orderQueue = Array();
public function placeOrder(Order $order) {
$orderQueue[] = $order;
$order->execute_order(array_shift($orderQueue));
}
}
class BuyStockOrder implements Order {
private $stock;
public function __construct(StockTrade $st) {
$this->stock = $st;
}
public function execute_order() {
$this->stock->buy();
}
}
class SellStockOrder implements Order {
private $stock;
public function __construct(StockTrade $st) {
$this->stock = $st;
}
public function execute_order() {
$this->stock->sell();
}
}
// the client
//class Client {
$stockName = 'AAPL';
$stock = new StockTrade($stockName);
$buyOrder = new BuyStockOrder($stock);
$sellOrder = new SellStockOrder($stock);
$agent = new Agent();
$agent->placeOrder($buyOrder);
$agent->placeOrder($sellOrder);
//}
?>