Hello guys, my name is Ridit Mukhopadhyay.
Today I will be discussing the command design pattern.
I have already discussed the command design pattern before in java and C++ but this time i did it in python.
Command Design Pattern is a design pattern in which there will be a piece of executable code encapsulated inside of some object - which is the command.
The Command is self sufficient and will have the knowledge of who has the skill or capability to execute it. While creating the command we bind it with the correct receiver who has the knowledge of how to execute the command.
Here’s my implementation of this design pattern in Python.
from from abc import ABC,abstractmethod
import time
class Item:
def __init__(self,name,price):
self.name = name
self.price = price
class ItemOrder:
def __init__(self,item,numberOfPlates):
self.item = item
self.numberOfPlate = numberOfPlates
class Order:
def __init__(self):
self.itemorders = []
def getItemOrder(self):
return self.itemorders
class ChefTeaAndCoffee:
def prepareTeaAndCoffee(self,order):
for itemorder in order.itemorders:
print(itemorder.numberOfPlate,"cups of ",itemorder.item.name,"is being prepared")
time.sleep(5)
class ChefFood:
def prepareFood(self,order):
for itemorder in order.itemorders:
print(itemorder.numberOfPlate,"plates of",itemorder.item.name,"is being prepared")
time.sleep(5)
class Command(ABC):
@abstractmethod
def execute(self):
pass
class CommandTeaAndCoffee(Command):
def __init__(self,chef: ChefTeaAndCoffee,order):
self.chef = chef
self.order = order
def execute(self):
self.chef.prepareTeaAndCoffee(self.order)
class CommandFood(Command):
def __init__(self,chef: ChefFood,order):
self.chef = chef
self.order = order
def execute(self):
self.chef.prepareFood(self.order)
class Waiter:
def setCommand(self,command):
self.command = command
def giveCommandToChef(self):
self.command.execute()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
itemTea = Item("Tea", 10)
itemCoffee = Item("Coffee", 25)
itemBread = Item("Bread", 60)
itemOrderTea = ItemOrder(itemTea, 3)
itemOrderCoffee = ItemOrder(itemCoffee, 4)
itemOrderBread = ItemOrder(itemBread, 6)
ordDrink = Order()
ordDrink.itemorders.append(itemOrderTea)
ordDrink.itemorders.append(itemOrderCoffee)
ordBread = Order()
ordBread.itemorders.append(itemOrderBread)
chefTeaCoffee = ChefTeaAndCoffee()
chefFood = ChefFood()
teaCoffeeCommand = CommandTeaAndCoffee(chefTeaCoffee, ordDrink)
foodCommand = CommandFood(chefFood, ordBread)
waiterTCoffee = Waiter()
waiterFoody = Waiter()
waiterTCoffee.setCommand(teaCoffeeCommand)
waiterFoody.setCommand(foodCommand)
waiterTCoffee.giveCommandToChef()
waiterFoody.giveCommandToChef()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
abc import ABC,abstractmethod
import time
class Item:
def __init__(self,name,price):
self.name = name
self.price = price
class ItemOrder:
def __init__(self,item,numberOfPlates):
self.item = item
self.numberOfPlate = numberOfPlates
class Order:
def __init__(self):
self.itemorders = []
def getItemOrder(self):
return self.itemorders
class ChefTeaAndCoffee:
def prepareTeaAndCoffee(self,order):
for itemorder in order.itemorders:
print(itemorder.numberOfPlate,"cups of ",itemorder.item.name,"is being prepared")
time.sleep(5)
class ChefFood:
def prepareFood(self,order):
for itemorder in order.itemorders:
print(itemorder.numberOfPlate,"plates of",itemorder.item.name,"is being prepared")
time.sleep(5)
class Command(ABC):
@abstractmethod
def execute(self):
pass
class CommandTeaAndCoffee(Command):
def __init__(self,chef: ChefTeaAndCoffee,order):
self.chef = chef
self.order = order
def execute(self):
self.chef.prepareTeaAndCoffee(self.order)
class CommandFood(Command):
def __init__(self,chef: ChefFood,order):
self.chef = chef
self.order = order
def execute(self):
self.chef.prepareFood(self.order)
class Waiter:
def setCommand(self,command):
self.command = command
def giveCommandToChef(self):
self.command.execute()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
itemTea = Item("Tea", 10)
itemCoffee = Item("Coffee", 25)
itemBread = Item("Bread", 60)
itemOrderTea = ItemOrder(itemTea, 3)
itemOrderCoffee = ItemOrder(itemCoffee, 4)
itemOrderBread = ItemOrder(itemBread, 6)
ordDrink = Order()
ordDrink.itemorders.append(itemOrderTea)
ordDrink.itemorders.append(itemOrderCoffee)
ordBread = Order()
ordBread.itemorders.append(itemOrderBread)
chefTeaCoffee = ChefTeaAndCoffee()
chefFood = ChefFood()
teaCoffeeCommand = CommandTeaAndCoffee(chefTeaCoffee, ordDrink)
foodCommand = CommandFood(chefFood, ordBread)
waiterTCoffee = Waiter()
waiterFoody = Waiter()
waiterTCoffee.setCommand(teaCoffeeCommand)
waiterFoody.setCommand(foodCommand)
waiterTCoffee.giveCommandToChef()
waiterFoody.giveCommandToChef()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/