|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
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.