aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes About Command Pattern Design attern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "About Command Pattern Design attern" Watch "About Command Pattern Design attern" New topic
Author

About Command Pattern Design attern

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi

I have read about Command Pattern , but i am getting confused on this .

I know that it means for For every action , a specific method is associated for that action.

Please tell me where actually the Command Pattern design pattern is used actually ?



Save India From Corruption - Anna Hazare.
James Dixon
Ranch Hand

Joined: Jun 20, 2009
Posts: 32
Hi Ravi

Wikipedia is a good source for finding out about the different patterns and what they do. You can find out more about it here.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

hey ,thank you .I understood the theory of it .

But can you please give me an example of using a Command Pattern in a Web Based Application .


Thanks in advance .
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Ravi Pavan wrote:
But can you please give me an example of using a Command Pattern in a Web Based Application .

We can use Command pattern in any application, not just web-based.

If you understand concepts, what is the point you do not understand?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


so can you tell me where exactly a Command Pattern is used . ( Can i think it this as an example consider a situation where there are a number of buttons like add , delete , update --- etc for every button there is a particuar method called .

Sorry for my english.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Command pattern decouples Invoker and Receiver. Invoker will only have dependency to Command interface, not concrete implementation.
It also able to support Undo/Redo. When execute a command we can keep it in a command stack, and after that we can undo/redo by getting a command from the stack and call undo/redo method.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Kengkaj Sathianpantarit wrote:Command pattern decouples Invoker and Receiver.


This is what i was expecting . Now can you please tell me what is meant by Invoker and Receiver in a web based application .

Thanks in advance.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
For example a servlet passes request to a command and the command delegate to a receiver (an object that knows how to handle the request). In this case the servlet acts as an invoker.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

That was Nice .Thank you very much sir.
ramesh vanka
Greenhorn

Joined: Mar 13, 2005
Posts: 11
Now I am explaning the command pattern:

1) Initial Program we have one sender and one receiver.
For example:
Receiver is Calculator:
Class Calculator{
add(){}
sub{}
mul{}
}

Sender is the Client:
-------------------------
//Client want add function in Calculator, He raises add request through calculator object.
Class Test{
Calculator calc = new Calculator();
//add request
calc.add();
//sub request
calc.sub():

}

}

In the above scenario, Client(Sender) directly calls the Calculator(Receiver) through sending request.

In the command design pattern:
----------------------------------------
Third Person will enter here, who takes the command from client(Sender) and do work with receiver(Calculator).
Sender --> Thirdparty --> Receiver.
Here Thirdparty means Invoker.
a) ThirdParty decouples the Sender and Receiver.


In command desing pattern 4 roles are there:
1) command
2) commandHoler
3) Invoker
4) Receiver

Intent:
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Encapsulate a request as an object means
Client wants add request:
In command design pattern, Receiver(Calculator) have functions/operations. For each/combination of operations we create speration commandHolder.

class AddCommandHolder implements Command{
**private Calculator calc ; // encapsulated receiver object

public AddCommandHolder(Calculator calc){
this.calc = calc;
}

public void execute(){
calc.add();
}

}

In general for add request --> calc.add
But in command pattern -->public void execute(){
calc.add();
}


class client{
Calculator calc = new Calculator();
//calc.add();--> this is for general thing.
AddCommandHolder add = new AddCommandHolder(calc);
}

AddCommandHolder: encapusulate my request in an object.
here encapusalted object is receiver object.

Command Holder --> encapuslate receiver object
Invoker --> encapsulate command object.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Command Pattern Design attern