O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Command Pattern

To command is to serve, nothing more and nothing less.

Andre Malraux

When you do the common things in life in an uncommon way, you will command the attention of the world.

George Washington Carver

Create like a god, command like a king, work like a slave.

Constantin Brancusi

What is the Command Pattern?

The command pattern allows a client to issue requests to an object without making any assumptions about the request, or the receiving object. Think of the request as a command sent to an object to engage in a known behavior. The straightforward way to do this would be to create an instance of the object, and call the method that implements the required command (or behavior). For example, let’s assume that we’re building a house that allows computer control of many of its components such as lights, doors, heating, etc. Let’s look at the code that would turn on a light bulb. The Light class implements a method called on() that turns on a light. A client would execute the following code to turn the light on.

var light = new Light();
light.on();

Let’s look at another command to open a door. In this case, the receiver of the command is an instance of the Door class, which implements a method called open() that opens the front door.

var frontdoor = new Door();
frontdoor.open();

Notice the tight coupling between the client and the receivers. By coupling, we mean the degree to which one section of code relies on another section. The client is tightly bound not only to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required