100 likes | 224 Views
Dive into the Command Pattern as discussed in the Head First Design Patterns. This pattern simplifies the development of home automation systems, such as remote controls for various devices like lights, TVs, and thermostats. By encapsulating requests as objects, it allows for parameterization of commands, queuing, logging, and even undo operations. Explore a diner example where commands like `burgerOrder()` and `shakeOrder()` demonstrate the flexibility and capabilities of this design pattern. Learn to create command objects, set commands in invokers, and execute various actions seamlessly.
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()