-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
56 lines (43 loc) · 1.23 KB
/
command.py
File metadata and controls
56 lines (43 loc) · 1.23 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
""""
Command Pattern with Python Code
"""
from abc import abstractmethod,ABCMeta
class Order(metaclass=ABCMeta):
@abstractmethod
def execute(self):
pass
class Stock():
_name = "ABC"
_quantity = 20
def buy(self):
print("Stock [Name :{0}, Quantity: {1}] bought.".format(self._name, self._quantity))
def sell(self):
print("Stock [Name :{0}, Quantity: {1}] sold.".format(self._name, self._quantity))
class BuyStock(Order):
_abcStock = None
def __init__(self, inStock):
self._abcStock = inStock
def execute(self):
self._abcStock.buy()
class SellStock(Order):
_abcStock = None
def __init__(self, inStock):
self._abcStock = inStock
def execute(self):
self._abcStock.sell()
class Broker():
_orderList = []
def takeOrder(self, inOrder):
self._orderList.append(inOrder)
def placeOrder(self):
for order in self._orderList:
order.execute()
self._orderList.clear()
if __name__ == "__main__":
stock = Stock()
buyAction = BuyStock(stock)
sellAction = SellStock(stock)
broker = Broker()
broker.takeOrder(buyAction)
broker.takeOrder(sellAction)
broker.placeOrder()