Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 22.5 ('Gyroscope')
Instant purchase and download via GumRoad!

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):

Download Icon 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.