Java Reference
In-Depth Information
Figure 8-1. The command pattern
Let's look at a concrete example of the command pattern and see how it improves with
lambda expressions. Suppose we have a GUI
Editor
component that has actions upon it that
functionality—that is, a series of operations that can be recorded and then run later as a
single operation. This is our receiver.
Example 8-1. Common functions a text editor may have
public
public interface
interface
Editor
Editor
{
public
public
void
void
save
();
public
public
void
void
open
();
public
public
void
void
close
();
}
In this example, each of the operations, such as
open
and
save
, are commands. We need a
generic command interface to fit these different operations into. I'll call this interface
Ac-
tion
, as it represents performing a single action within our domain. This is the interface that
all our command objects implement (
Example 8-2
).
Example 8-2. All our actions implement the Action interface
public
public interface
interface
Action
Action
{
public
public
void
void
perform
();