This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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
posted
0
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.
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
posted
0
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.
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.