Java Reference
In-Depth Information
CHALLENGE 24.2
Fill in the code for the anonymous subclasses of ActionListener , overriding
actionPerformed() , noting that this method expects an ActionEvent
argument.
When you outfit a menu with commands, you are plugging your commands into a context
provided by another developer. In other cases of C OMMAND , you will take the role of the
context developer, providing the context in which commands will execute. For example, you
might want to provide a timing service that records how long methods take to execute.
Using Command to Supply a Service
Suppose that you want to let developers time how long a method takes to execute. Figure 24.1
shows a Command interface and a time() utility that times the execution of a command.
Figure 24.1. The time() method returns the number of milliseconds that a command
takes to execute.
The code for time() captures the system time before and after executing the command and
returns the difference:
public static long time(Command c)
{
long t1 = System.currentTimeMillis();
c.execute();
long t2 = System.currentTimeMillis();
return t2 - t1;
}
Suppose that you decide to test the time() method with an automated testing tool. One
reasonable test is that a command that sleeps for, say, 2,000 milliseconds should take about
2,000 milliseconds to execute:
Search WWH ::




Custom Search