From the course: Advanced Python: Build Hands-On Projects with Design Patterns (2023)
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Command pattern example - Python Tutorial
From the course: Advanced Python: Build Hands-On Projects with Design Patterns (2023)
Command pattern example
- [Instructor] Let's start by defining our command class which has an abstract method called execute. Type def, then the name of the method, execute. Let's put self here. Please note that the only method the command class has is the execute method that serves as an interface used to execute various self-contained operations. It doesn't do anything by itself. Next, let's define some concrete command classes. The first one is Copy. This class wraps a copy operation. Type def execute, parentheses, self, colon, print. The message is copying. We have two other concrete command classes, Paste and Save. All these concrete classes do is printing messages, showing what they're doing. For example, the Copy concrete class prints a message showing that it is copying. Now, we are ready to create our Invoker class. The Invoker class creates multiple concrete command objects and stores them in a list. We call our Invoker class…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.