• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remote Control Analogy to Command Pattern

 
Greenhorn
Posts: 4
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After reading Head Fisrt Design Pattern, I am able to summarize following analogy :
A Button in a remote control => a ConcreteCommand
An Electrical Appliance => Receiver
RemoteAControl => Client (my undersatnding)
RemoteAControl => Invoker (as per HFDP)
A Slot in RemoteControl(corresponding to a set of buttons specific to an appliance) => Invoker , can be parameterized with different requests


Please comment if there is a mismatch in the analogy. Also I have few questions regarding Comamnd pattern :
Is invoker a logical entity ?
What is the role played by a remote control in Command pattern – Invoker, Client ? The book mentions it as Invoker. Then who is playing role of the client other than a class having public static void main ?
Why do we have undo as a shared global command ? Each Command object has its own implementation of undo() method and actually knows how to undo last request.

 
Lalit m Upadheyay
Greenhorn
Posts: 4
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why do we have undo as a shared global command ? Each Command object has its own implementation of undo() method and actually knows how to undo last request.



It was a design decision. We can keep an array of dedicated undoCommand objects that would be occupying space in heap memory or we can keep a shared undo command object without much memory overhead. Keeping a dedicated array of undo command would serve only one purpose - ability to undo any sequence of execution of command in any order.
The shared undo command will be initialized to the latest Command object(just after its execute is invoked) .
The sole purpose of undo command is to undo execution effect of latest command object. and that would surely mean redo as well if last command was kind of delete, close, switchoff, stop requests.
If we just press undo without any command being in effect, nothing would happen as all command objects have been initialized with NoCommand( a Null Object pattern).
If we press undo after a Command execution is in effect, its execution will be reverted and last available state will be re-stored.
If we press undo consecutively second or third time after a Command execution was in effect, nothing would happen as the effect has already been reverted on the very first execution of undo command.

I am able to analyze some of above questions - RemoteControl is an Invoker. An Invoker holds reference to command and at some point asks the command to carry out a request by calling its execute() method.
The client creates Invoker Object, Receiver Objects, Command Objects, binds the Receiver to the Command and adds reference of Comamnd objects to the Invoker Object. Later it uses the Invoker object to carry out specific request. So a Client class having public void static main is fine unless we are really looking for a fancier client .


Still trying to resolve queries around slot on the remote. Can someone please try to analyze with me ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic