Java Reference
In-Depth Information
}
We can now implement our
Action
interface for each of the operations. All these classes
need to do is call a single method on our
Editor
and wrap this call into our
Action
inter-
face. I'll name the classes after the operations that they wrap, with the appropriate class nam-
8-4
are our command objects.
Example 8-3. Our save action delegates to the underlying method call on Editor
public
public class
class
Save
Save
implements
implements
Action
{
private
private final
final
Editor editor
;
public
public
Save
(
Editor editor
) {
this
this
.
editor
=
editor
;
}
@Override
public
public
void
void
perform
() {
editor
.
save
();
}
}
Example 8-4. Our open action also delegates to the underlying method call on Editor
public
public class
class
Open
Open
implements
implements
Action
{
private
private final
final
Editor editor
;
public
public
Open
(
Editor editor
) {
this
this
.
editor
=
editor
;
}
@Override
public
public
void
void
perform
() {
editor
.
open
();
}
}
Now we can implement our
Macro
class. This class can
record
actions and run them as a
group. We use a
List
to store the sequence of actions and then call
forEach
in order to ex-