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.

That was almost 2 years ago.

Now again as my son is planning to delve into Fluid Simulation, there are some software in which the scenes are developed in Python.

Hence, again I am trying to open the PyCharm and practicing Python.

Here is 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, order):
        pass

class ChefTeaCoffe(Chef):
    def prepare(self, order):
        for itemorder in order.itemorders:
            if "Tea" in itemorder.item.name:
                print(f"{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(f"{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.

0
Subscribe to my newsletter

Read articles from Somenath Mukhopadhyay directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Somenath Mukhopadhyay
Somenath Mukhopadhyay

To win is no more than this... To rise each time you fall...