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. ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.