17
Command Pattern Encapsulation Invocation

Command Pattern

  • Upload
    uzuri

  • View
    63

  • Download
    0

Embed Size (px)

DESCRIPTION

Command Pattern. Encapsulation Invocation. One size fits all. Vender Classes. Intro to Command Pattern. Interaction in detail. Encapsulation. An order Slip encapsulates a request to prepare a meal. The waitress's job is to take order Slips and invoke the orderUp () method on them. - PowerPoint PPT Presentation

Citation preview

Page 1: Command Pattern

Command Pattern

Encapsulation Invocation

Page 2: Command Pattern

One size fits all

Page 3: Command Pattern

Vender Classes

Page 4: Command Pattern

Intro to Command Pattern

Page 5: Command Pattern

Interaction in detail

Page 6: Command Pattern

Encapsulation

• An order Slip encapsulates a request to prepare a meal.

• The waitress's job is to take order Slips and invoke the orderUp() method on them.

• The Cook has the knowledge required to prepare the meal.

Page 7: Command Pattern

API interface??

Page 8: Command Pattern

From Dinner to Command Pattern

Page 9: Command Pattern

Our first command objectpublic interface Command {

public void execute();}

Implementing Command to turn light onpublic class LightOnCommand implements Command { public LigthOnCommand(Light light) { this.light = light; } public void execute(){ light.on(); }}

Light

on()off()

Page 10: Command Pattern

Using Command object

public class SimpleRemoteControl {Command slot;public SimpleRemoteControl() { }

public void setCommand(Command command) {slot = command;

} public void buttonWasPresses() { slot.execute(); }}

Page 11: Command Pattern

Creating a simple testpublic class RemoteControlTest {

public static void main(String[] args) {SimpleRemoteControl remote = new SimpleRemoteControl();Light light = new Light();GarageDoor garageDoor = new GarageDoor();LightOnCommand lightOn = new LightOnCommand(light);GarageDoorOpenCommand garageOpen = new

GarageDoorOpenCommand(garageDoor); remote.setCommand(lightOn);

remote.buttonWasPressed();remote.setCommand(garageOpen);remote.buttonWasPressed(); }

}

Page 12: Command Pattern

Command Pattern Defined

The Command Pattern encapsulates a request as an object; thereby letting you parameterize other objects with

different requests, queue or log requests, and support undoable

operations.

Page 13: Command Pattern
Page 14: Command Pattern
Page 15: Command Pattern
Page 16: Command Pattern

public class RemoteControl {Command[] onCommands;Command[] offCommands;

public RemoteControl() {onCommands = new Command[7];offCommands = new Command[7];

Command noCommand = new NoCommand();for (int i = 0; i < 7; i++) {onCommands[i] = noCommand;offCommands[i] = noCommand; } }

public void setCommand(int slot, Command onCommand, Command offCommand) {onCommands[slot] = onCommand;offCommands[slot] = offCommand; }

public void onButtonWasPushed(int slot) {onCommands[slot].execute(); }

public void offButtonWasPushed(int slot) {offCommands[slot].execute(); } }}

Page 17: Command Pattern