The Command Pattern

Remember the good old days, before high speed internet existed? (They were terrible times trust me) You would sit at home and if you were lucky, maybe you had sixty or channels to watch. Pointing your remote at the screen mindlessly hitting up and down until you found something interesting. Channel surfing I think they called it.

Did you ever wonder how your remote works? Well today we are going to look at a simple example of what some of the code might look like for a remote by talking about the command pattern.

The command pattern is great to use when you know what action you want to invoke on your receiver. The receiver in this example will be the television. Lets use a simple example of wanting to turn our television on and off.

Next lets take a look at our command interface.

The command interface is just one method called execute. It is generic and simple on purpose because the commands that implement this interface will define what execute does. We can have many concrete commands that implement this interface, such as volume up, volume down, back to previous channel, but today we will only be talking about turning the TV on and off.

Lets look at an example of what our concrete command for turning television on and off will look like.

We give this on command the instance of a television it will be turning on, we will use that same instance of a television for the off command as well. That way the on and off commands are linked to one television.

The thing that will call these command is called the invoker. The invoker in this example will be the remote. This is the object that executes the command. The invoker often will do bookkeeping about the command execution. Usually remotes have a previous button that will take you to the previous channel, so an invoker would keep track of the last channel. Also things like tv volume, and the current channel.

As you see in the constructor we would pass in instances of the on command and off command that are linked to our television.

All these things would get tied together by what is called a client. This might be the main class in our Java application. That way we can create our objects, pass them in as constructors where they need to go, and call the commands on the invoker.

Television television = new Television();
Command onCommand = new OnCommand(television);
Command offCommand = new OffCommand(television);
Remote remote = new Remote(onCommand, offCommand);

Then to turn our tv on we would simply call this

// This would print out "The television is on prepare to numb your brains!"

Remember the command pattern chain of events goes like this, the client -> the invoker -> the concrete command -> the receiver.

Command pattern is great for modularity, helping us following the open closed principle, and it gives us a lot of interchangeable parts. We could of even had a interface for the receiver which is the television, because all it implements is a turnOff and turnOn method.

Thanks for reading!