Article Preview
Buy Now
FEATURE
Design Patterns 6—Supporting Undo
Implementing the Command Pattern
Issue: 22.5 (September/October 2024)
Author: Marc Zeedar
Author Bio: Marc taught himself programming in high school when he bought his first computer but had no money for software. He's had fun learning ever since.
Article Description: No description available.
Article Length (in bytes): 38,186
Starting Page Number: 56
Article Number: 22505
Resource File(s):
project22505.zip Updated: 2024-09-02 10:58:47
Related Link(s): None
Excerpt of article text...
In this issue of my series on Design Patterns, I'm going to cover the Command Pattern. This is my most complex pattern to date, but it works amazingly well and is extremely useful and powerful.
What is the Command Pattern?
Basically, it's a way to package an action into a self-contained object. This object can then be sent to other objects, queued for delayed execution, and, most importantly, it can be used for easy undoable operations.
The Command Pattern can also consolidate all your actions into a single place—a collection of action classes—rather than having action code scattered throughout different parts of your project. This is particularly beneficial when a program's user interface might provide multiple ways for the same action to be executed.
For instance, a word processing app might have an action that italicizes the selected text. This task could be accomplished via a menu, a toolbar button, a contextual menu, a script, and so on. They all perform the exact same function and produce the same result, but there are different ways of triggering the action.
You'd never write the same code six different times for six different ways of executing the action, of course—you'd create a method that gets called to perform the task (i.e.,
makeTextBold
). But where do you put that method? Should it be part of the window? In a global module? As part of a data structure class?
...End of Excerpt. Please purchase the magazine to read the full article.