21

The Command Pattern

The Chain of Responsibility pattern forwards requests along a chain of classes, but the Command pattern forwards a request only to a specific object. It encloses a request for a specific action inside an object and gives it a known public interface. It lets you give the client the ability to make requests without knowing anything about the actual action that will be performed. It also allows you to change that action without affecting the client program in any way.

We have, of course, seen this command interface already, as we have been using in our DButton class, which includes a comd method that is called when you click the button.

When to Use the Command Pattern

When you build a Python user interface, you provide menu ...

Get Python Programming with Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.