CS 210 Introduction to Design Patterns September 26 th, 2006
Head First Design Patterns Chapter 6 Command Pattern Encapsulating Invocation
Motivating problem description Build a remote that will control variety of home devices Sample devices: lights, stereo, TV, ceiling light, thermostat, sprinkler, hot tub, garden light, ceiling fan, garage door
Introducing the command pattern – Diner example Burger & Shake Order takeOrderorderUp Make Burger & Shake createOder() takeOder() orderUp() make_X()
Introducing the command pattern Create Command Object execute()setCommandexecute() action1 action2 creatCommandObject() setCommand() execute() action_X()
Command Pattern for home automation action() execute(){ receiver.action() } execute() An encapsulated Request Invoker
Simple Remote Control Eclipse example
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.
Command Pattern Class Diagram ClientInvoker setCommand() > Command execute() undo() Receiver action() ConcreteCommand execute() undo()
Command Pattern Class Diagram for Home automation RemoteLoader RemoteControl onCommands offCommands setCommand() onButtonPushed() offButtonPushed() > Command execute() undo() Light on() off() LightOnCommand execute() undo() LightOffCommand execute() undo()