课程: Advanced Python: Build Hands-On Projects with Design Patterns (2023)
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Command pattern example - Python教程
课程: 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…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。