Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command Pattern - A Command for each Action

 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Are we going to have a specific Command for each Receiver(Action).
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends. A Command could execute one of the Receiver's actions, but it could just as easily execute multiple actions depending, for instance, on the granularity of the actions.
A Command doesn't have to be specific to one concrete Receiver implementation, either.
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:A Command doesn't have to be specific to one concrete Receiver implementation, either.


You mean that there can exist an implementation of a Command which has reference NOT to one concrete Receiver implementation but to it's interface. By progamming to an interface we can decouple Command and Receiver and a chance to call multiple Receiver implementations.
But on the other hand, the Reveiver implementations needn't be similar at all, they can be completely two different objects with different behaviors. In this case we cannot program to an interface and need to have two different Command implementations.
Am I right here?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep
 
krishna prasad gunasekaran
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thanks, but i have another one for you..
Struts say they use Command Pattern calling Action's execute() method. But i couldn't come around the explanation that a very basic simple use of inheritance and polymorphism can be called a pattern instead of a programming idiom. I mean, all request processor does is action.execute(), which by the way, just uses an interface reference to call the method. This is so very basic programming technique. can this be called Command Pattern.
Command Pattern, as far as i have understood, achieves complete decoupling of Invoker and Receiver. Meaning, the Invoker can invoke any Object, not just implementations of Action. All it care is to call Comamnd's execute(), which does the rest. But in struts this isn't the case, i guess.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic