课程: Advanced Python: Build Hands-On Projects with Design Patterns (2023)

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Command pattern

Command pattern

- [Instructor] There are situations when it's necessary to wrap a request in an object. In an object-oriented language like Python, the request comes in the form of a method. The command pattern addresses this need and provides guidance on how to best build an object whose sole purpose is to package a method that can be easily passed around. The structure of the command pattern is straightforward. It features a parent class called command, which provides an interface that allows you to execute a method. Users of the command pattern write their own concrete commands which inherit from the command class. One of the reasons you need to encapsulate a method is to delay or queue the execution of an action on a receiver object. This capability is especially useful when you need to dynamically compose a sequence of behavior or undo actions. By the way, the word encapsulate here is a fancy way of saying wrapping. To…

内容