ActionScript 3.0 Design Patterns

(Chris Devlin) #1
What Is the Command Pattern? | 249

task. Figure 7-1 shows the household equivalent of a command interface. Mom’s


command interface is a note with the operative word “do,” which will eventually


describe what chore needs to be done, and who will do it.


Mom creates severalconcrete commandsthat conform to thecommand interfacefor


the household tasks that need to be done while she is away. She puts notes, assigning


each task to a different person, inside four envelopes. When the envelopes are sealed,


it’s not possible to tell which kid’s responsibility it is to do the tasks, or even what


tasks are enclosed in the envelopes. All we know is that the envelope contains a task.


Therefore, the receiver and the task are hidden orencapsulated within the envelope.


Figure 7-2 shows Mom’s concrete commands that implement her command inter-


face declared in Figure 7-1. The four concrete commands: John willload the dish-


washer, Jane willwalk the dog, Jack willdo the laundry, and Dad willclean the garage


(Dad won’t know what hit him). Mom has assigned each task to the person most


appropriate to carry it out. She knows that Jane is the best person to walk the dog, as


Brutus is on his best behavior when Jane is around. Dad is the best person to clean


the garage, as it is his mess in the first place, and so on and so forth.


Figure 7-1. Mom’s command interface


Figure 7-2. Household equivalent of concrete commands

Free download pdf