O'Reilly logo

Python Unlocked by Arun Tigeraniya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Command pattern

Key 8: Easy-execution management for commands.

In this pattern, we encapsulate information that is needed to execute a command in an object so that command itself can have further capabilities, such as undo, cancel, and metadata that are needed at a later point of time. For example, let's create a simple Chef in a restaurant, users can issue orders (commands), commands here have metadata that are needed to cancel them. This is similar to a notepad app where each user action is recorded with an undo method. This makes coupling loose between caller and the invoker, shown as follows:

import time import threading class Chef(threading.Thread): def __init__(self,name): self.q = [] self.doneq = [] self.do_orders = True threading.Thread.__init__(self,) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required