• 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

About Command Pattern Design attern

 
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was Nice .Thank you very much sir.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
reply
    Bookmark Topic Watch Topic
  • New Topic