Receiver's interface.) The term request here refers to the command that is to be executed. The Command pattern also allows us to vary when and how a request is fulfilled.
Therefore, a Command pattern provides us flexibility as well as extensibility.In programming languages like C, function pointers are used to eliminate giant switch statements. (See "Java Tip 30: Polymorphism and Java" for a more detailed description.) Since Java doesn't have function pointers, we can use the Command pattern to implement
callbacks. You'll see this in action in the first code example below, called TestCommand.java.
Developers accustomed to using function pointers in another language might be tempted to use the Method objects of the Reflection API in the same way. For example, in his article "Java Reflection," Paul Tremblett shows you how
to use Reflection to implement transactions without using switch statements. I've resisted this temptation, since Sun advises
against using the Reflection API when other tools more natural to the Java programming language will suffice. (See Resources for links to Tremblett's article and for Sun's Reflection tutorial page.) Your program will be easier to debug and maintain
if you don't use Method objects. Instead, you should define an interface and implement it in the classes that perform the needed action.
Therefore, I suggest you use the Command pattern combined with Java's dynamic loading and binding mechanism to implement function pointers. (For details on Java's dynamic loading and binding mechanism, see James Gosling and Henry McGilton's "The Java Language Environment -- A White Paper," listed in Resources.)
By following the above suggestion, we exploit the polymorphism provided by the application of a Command pattern to eliminate
giant switch statements, resulting in extensible systems. We also exploit Java's unique dynamic loading and binding mechanisms
to build a dynamic and dynamically extensible system. This is illustrated in the second code sample example below, called
TestTransactionCommand.java.
The Command pattern turns the request itself into an object. This object can be stored and passed around like other objects.
The key to this pattern is a Command interface, which declares an interface for executing operations. In its simplest form, this interface includes an abstract
execute operation. Each concrete Command class specifies a receiver-action pair by storing the Receiver as an instance variable. It provides different implementations of the execute() method to invoke the request. The Receiver has the knowledge required to carry out the request.
excellent topicBy Anonymous on November 10, 2009, 4:45 amvery very informative and cleared all my doubts. Thank you very much for your effort
Reply | Read entire comment
political scienceBy Anonymous on March 4, 2009, 7:13 amhow to learn plz help me actully i never remember the thing nd topic.
Reply | Read entire comment
Check the datesBy Athen on February 28, 2009, 2:07 amThis Java Tip was published in 1999; Head First Design Patterns came out in 2004. Athen
Reply | Read entire comment
Not Very GoodBy Anonymous on February 26, 2009, 10:56 amI don't see that this article credits the book "Head First Design Patterns" The examples are nearly identical. They invented the switch to explain the command...
Reply | Read entire comment
Very GoodBy Anonymous on January 13, 2009, 9:59 pmHI This is very good topic and neatly explained. The Code cleared lot of doubts as to how it actually works. But please i cant load the class from the Command Manager...
Reply | Read entire comment
View all comments