Command Design Pattern

Command design pattern is used to encapsulate a request as an object and pass to an invoker, wherein the invoker does not knows how to service the request but uses the encapsulated command to perform an action.

Command is an interface with execute method. It is the core of contract.

A client creates an instance of a command implementation and associates it with a receiver.

An invoker instructs the command to perform an action.

A Command implementation’s instance creates a binding between the receiver and an action.

Receiver is the object that knows the actual steps to perform the action.

Command Pattern Discussion

It will be easier to understand all the above with an example. Let us think of developing a universal remote. Our UnversalRemote will have only two buttons, one is to power on and another is to mute all associated ConsumerElectronics.

For ConsumerElectronics we will have couple of implementations Television and SoundSystem. Button is a class that is the invoker of an action.

OnCommand is used to switch on a ConsumerElectronics. While instantaiting OnCommand client needs to set the receiver. Here receiver is either Television or SoundSystem. UniversalRemote class will maintain all the receivers and will provide a method to identify the currently active receiver.

When an instance of a invoker is created, a concrete command is passed to it. Invoker does not know how to perform an action. All it does is honoring the agreement with the Command. Invoker calls the execute() method of the set concrete command.

hi,
all
I want to maintain the state of checkboxes across pages.
What happens is when i check one checkbox and then go to say 2nd page then check one checkbox there and when i return to previous page the checked box is again unchecked.
Means the status of the checkbox is not maintained.If it is maintained then it should show checked.

I am using jsps and display tag in jsp for pagination.
Any kind of help would be greatly appreciated.

You need pass on flag which pass on the state of the check box when you navigate from one page to another page and vise visa if there are any change in the check box status then flag has to be marked accordingly .Hope this clue will help you

Your articals are really just awsome.
I really am able to call back Einsteins Quote:”Everything should be made as simple as possible, but no simpler.”
The way you explain was really cool.
I am still in between reading your blogs on design patterns..

Can u please give a clarification on how to create dynamic objects along with new instace of a class?
If it is explained in any of the design patterns please ping me that link..i’ll go through it.

What am expecting is something like this.

for(;;){
Pavan p=new Pavan();//Pavan is a class
}

At the end what i want to acheive is to find the number of instances of that particular class.

Via RMI/JMS/Web service we need to either send the object from other vm or something else…but we cannot jst browse through the objects in another JVM..Please correct me about what i am trying to saying is meaningful…

The article was good, but it keeps some questions unanswered like,
Why do we really create this complex structure ? When UniversalRemote already has access to ConsumerElectronics, why not directly invoke ConsumerElectronics.on() and ConsumerElectronics.muteAll() methods, when the appropriate button is clicked, what is the advantage of creating the intermediate classes like Command ?

If aim is to keep the receiver and invoker of command decoupled from each other, intermediary interface is needed.

Most of the design pattern based on this design principal. “Classes should be open for extension, but closed for modification”.

If I have to add ConsumerElectricals to be switched on from the same Button class, then the scenario that have been suggested by you ask me to modify the existing button implementation which violates above rule. However with the Command patters design would be like this.

This pattern is too complex or confusing… The examples given here are very nice and could understand but I was not able to correlate exactly on how it is implemented in JDK – Runnable and Action implementations.