Whereas some of the patterns I’ve covered so far have felt more like broad strategy, the Command Pattern has a very narrow and specific focus. It is used to create an object that has one specific task and simply waits until it is executed. It’s built upon a principle that we’ve seen in other patterns too: to separate out the things that change from the things that stay the same.

Olsen’s example in Design Patterns that quickly made sense for me was a GUI framework that had various buttons defined and styled. If you wanted to use these buttons in your application you wouldn’t want to duplicate all that button setup code every time you needed a button and then just change the method for:

def on_button_push
  # some action for this time
end 

Instead, what you could do is create a separate class that held the action, and pass that new class to your button class upon creation. For example, your new class to send a message would look like this:

class SendCommand
  def execute
    send_the_message
  end
end

…and then when you wanted to use the already defined button in your app you could create it like so:

send_btn = ButtonClass.new(SendCommand.new)

The Button Class would just need to initialize a command attribute for the command you passed it, and then it’s method for calling the action would be:

def on_button_push
  @command.execute
end

(Lots of props to Olsen because my example is almost identical to his but it just painted the picture so clearly that I didn’t want to mess with it.)

The Command Pattern makes so much sense to me after this example that I wondered why I hadn’t come across it before now. However, Olsen states that “There is something about the Command pattern that seems to invite enthusiastic overuse”, and I could certainly see where a newcomer like myself could get carried away.

Olsen includes a whole other section on how you can use the Command Pattern to record steps that a user has taken to track state and then undo and redo commands as well, but I’m going to leave that for another post. In the meantime, while I work on my Limelight app this week I’m going to dive into the Limelight source code (a GUI framework) and see if I can hunt down some Command Pattern examples in the wild.