Tuesday, March 7, 2023

The Command Pattern in Python...

When I taught my son #Ridit and many other students about Python, I had to cover the subject of the Design Patterns implemented in Python.

Here we go. - the Command Pattern being implemented in Python.

We have taken the same Restaurant problem which has Chef, Waiter, and Order classes participating in this design pattern.

The idea of Command Pattern is that the decoupling between the invoker of the command and the executor.

The command is kind of a full-fledged object having all knowledge about the executor of the command meaning the command will have a reference to the executor of the command.

The waiter is working as the invoker and the chef is working as the executor of the command.

Here we go.

The Source Code


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.numberOfPlates = numberOfPlates
class Order:
    def __init__(self):
    	self.itemorders = []
    def getItemOrder(self):
        return self.itemorders
#executor
class Chef(ABC):
    @abstractmethod
    def prepare(self):
        pass


class ChefTeaCoffe(Chef):
    def prepare(self,order):
        for itemorder in order.itemorders:
            if "Tea" in itemorder.item.name:
                print(str(itemorder.numberOfPlates) + " cups of " + itemorder.item.name + " is being prepared")
                time.sleep(5)
                

class ChefFood(Chef):
    def prepare(self,order):
        for itemorder in order.itemorders:
            if "Food" in itemorder.item.name:
                print(str(itemorder.numberOfPlates) + " plates of " + itemorder.item.name + " is being prepared")
                time.sleep(5)
                
#command
class Command(ABC):
    @abstractmethod
    def execute(self):
        pass
class TeaCoffeeCommand(Command):
    def __init__(self,chef,order):
        self.chef = chef
        self.order = order

    def execute(self):
        self.chef.prepare(self.order)

class FoodCommand(Command):
    def __init__(self,chef,order):
        self.chef = chef
        self.order = order

    def execute(self):
        self.chef.prepare(self.order)
#invoker
class Waiter:
    def setCommand(self,command):
        self.command = command

    def giveCommandToChef(self):
        self.command.execute()


if __name__ == '__main__':
    itemTea = Item("Tea",40)

    itemBread = Item("Food", 20)

    itemOrderTea = ItemOrder(itemTea, 3)

    itemOrderBread = ItemOrder(itemBread, 4)

    orderTea = Order()
    orderFood = Order()

    orderTea.itemorders.append(itemOrderTea)
    orderFood.itemorders.append(itemOrderBread)

    chefTea = ChefTeaCoffe()
    chefFood = ChefFood()

    commandTeaCoffee = TeaCoffeeCommand(chefTea, orderTea)
    commandFood = FoodCommand(chefFood, orderFood)

    waiter = Waiter()
    waiter.setCommand(commandTeaCoffee)
    waiter.giveCommandToChef()

    waiter.setCommand(commandFood)
    waiter.giveCommandToChef()

Here is my son, Ridit, explaining the command design pattern implemented in Python.
I hope you will like it.



No comments: