Python Design Patterns Tutorial
Python Design Patterns Resources
Selected Reading
- Exception Handling
- Python Design Patterns - Anti
- Concurrency in Python
- Strings & Serialization
- Python Design Patterns - Queues
- Python Design Patterns - Sets
- Lists Data Structure
- Dictionaries
- Python Design Patterns - Iterator
- Object Oriented Concepts Implementation
- Object Oriented
- Abstract Factory
- Python Design Patterns - Flyweight
- Python Design Patterns - Template
- Python Design Patterns - Strategy
- Python Design Patterns - State
- Python Design Patterns - Observer
- Chain of Responsibility Pattern
- Python Design Patterns - Proxy
- Python Design Patterns - Decorator
- Python Design Patterns - Adapter
- Python Design Patterns - Command
- Python Design Patterns - Facade
- Python Design Patterns - Prototype
- Python Design Patterns - Builder
- Python Design Patterns - Factory
- Python Design Patterns - Singleton
- Model View Controller Pattern
- Python Design Patterns - Gist
- Introduction
- Python Design Patterns - Home
Python Design Patterns Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Design Patterns - Command
Python Design Patterns - Command
Command Pattern adds a level of abstraction between actions and includes an object, which invokes these actions.
In this design pattern, cpent creates a command object that includes a pst of commands to be executed. The command object created implements a specific interface.
Following is the basic architecture of the command pattern −
How to implement the command pattern?
We will now 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 above program generates the following output −
Explanation
The output implements all the commands and keywords psted in Python language. It prints the necessary values of the variables.
Advertisements