100 likes | 216 Views
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
E N D
CS 210 Introduction to Design Patterns September 26th, 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
Order orderUp takeOrder Make Burger & Shake Introducing the command pattern – Diner example createOder() Burger & Shake Order Slip Customer takeOder() Waiter orderUp() Command Cook make_X()
execute() execute() Create Command Object setCommand action1 action2 Introducing the command pattern creatCommandObject() Command Client setCommand() Invoker execute() Command Receiver action_X()
action() Receiver RemoteSlot execute() execute() execute() LightOnCommand GarageDoorOpen StereoOff Command Pattern for home automation An encapsulated Request execute(){ receiver.action() } Invoker Command
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.
Client Invoker Receiver <<Interface>> Command setCommand() action() execute() undo() Command Pattern Class Diagram ConcreteCommand execute() undo()
Light <<Interface>> Command execute() undo() LightOnCommand LightOffCommand execute() undo() execute() undo() Command Pattern Class Diagram for Home automation RemoteControl RemoteLoader onCommands offCommands setCommand() onButtonPushed() offButtonPushed() on() off()