Design Patterns – Command in Python

Jigisha Sata

12 hours ago

Python Design Patterns
Python Design Patterns
Table of Content
  • Implementation of the command pattern?
  • Output
  • Explanation
It adds a level of abstraction between actions and includes an object, which invokes these actions.
In command design pattern, the client creates a command object that includes a list of commands to be executed.
The basic architecture of the command, the pattern is shown in the figure below.
Basic Architecture of the command pattern
Basic Architecture of the command pattern
Implementation of the command pattern?
Now let’s see how to implement the design pattern.
def demo(a,b,c):
   print('a:',a)
   print('b:',b)
   print('c:',c)

class Command:
   def __init__(self, cmd, *args):
      self._cmd=cmd
      self._args=args

   def __call__(self, *args):
      return apply(self._cmd, self._args+args)
cmd = Command(dir,__builtins__)
print (cmd())

cmd = Command(demo,1,2)
cmd(3)


Output
The output is shown below:
Output for design pattern
Output for design pattern
Explanation
     The above code implements all the commands and keywords listed in Python language. It also prints all the necessary values of the variables as per the requirement.
To know more about Design Patterns - command in python join our courses.
I hope you enjoyed reading this article and finally, you came to know about Design Patterns – Command in Python
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review