From the course: Python: Advanced Design Patterns

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Command

Command

- [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 cue the execution of an action on a receiver object. This capability 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 reiterate, the concrete commands are our mechanism for invoking an action on a…

Contents