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,)
self.name = name
self.start()
def makeorder(self, order):
print("%s Preparing Menu :"%self.name )
for item in order.items:
print("cooking ",item...