• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Command Pattern Design attern

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?


 
James Dixon
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was Nice .Thank you very much sir.
 
ramesh vanka
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic