Well, I will contribute with the little experience I have, since I have seen in very few cases this type of design, due to the little need of it (as already mentioned by Jefferson).
At first, Python basically already has a command built-in function, which is as follows (Example extracted from stack overflow in English - Here ):
def greet(who):
print "Hello %s" % who
greet_command = lambda: greet("World")
greet_command()
However, I've seen applications using the pattern "Command" on occasions from:
- Commands need to perform more than simply execute
a basic function / invoked;
- Increased legibility in some extended cases.
It is obvious, however, that cases of commands are often cases of a single method and simple / direct execution, leaving any approach different from the first block of code unnecessary.
Stackoverflow's own English post is a good example, as in cases where you need to take actions and eventually reverse them:
Class ComandoGenerico():
def __init__(self, destino, origem):
self.origem = origem
self.destino = destino
#primeira alteração
os.rename(self.origem, self.destino)
#função para desfazer o comando inicial
def desfazer(self):
os.rename(self.destino, self.origem) #reverte para a condição inicial do comando
pilha = []
pilha.append(ComandoGenerico("oi.csv", "tudo_bem.csv")) #Arquivo renomeado para "tudo_bem.csv"
pilha.append((ComandoGenerico("tudo_bem.csv", "okay.csv")) #Arquivo renomeado para "okay.csv"
pilha.pop().desfazer() #Comando desfeito - Arquivo renomeado para "tudo_bem.csv"
pilha.pop().desfazer() #Comando desfeito - Arquivo renomeado para "oi.csv"