在软件开发系统中,常常出现“方法的请求者”与“方法的实现者”之间存在紧密的耦合关系。这不利于软件功能的扩展与维护。例如,想对行为进行“撤销、重做、记录”等处理都很不方便,因此“如何将方法的请求者与方法的实现者解耦?”变得很重要,命令模式能很好地解决这个问题。
在现实生活中,这样的例子也很多,例如,电视机遥控器(命令发送者)通过按钮(具体命令)来遥控电视机(命令接收者),还有计算机键盘上的“功能键”等。
什么是命令(Command)模式?
命令(Command)模式的定义如下:将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行储存、传递、调用、增加与管理。
命令模式的主要优点如下。
降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。可以实现宏命令。命令模式可以与组合模式结合,将多个命令装配成一个组合命令,即宏命令。方便实现 Undo 和 Redo 操作。命令模式可以与后面介绍的备忘录模式结合,实现命令的撤销与恢复。
其缺点是:可能产生大量具体命令类。因为计对每一个具体操作都需要设计一个具体命令类,这将增加系统的复杂性。
例子
public class App
{
/**
* Program entry point.
*
* @param args
command line args
*/
public static void main
(String
[] args
) {
var wizard
= new Wizard
();
var goblin
= new Goblin
();
goblin.printStatus
();
wizard.castSpell
(new ShrinkSpell
(), goblin
);
goblin.printStatus
();
wizard.castSpell
(new InvisibilitySpell
(), goblin
);
goblin.printStatus
();
wizard.undoLastSpell
();
goblin.printStatus
();
wizard.undoLastSpell
();
goblin.printStatus
();
wizard.redoLastSpell
();
goblin.printStatus
();
wizard.redoLastSpell
();
goblin.printStatus
();
}
}
/**
* Interface
for Commands.
*/
public interface Command
{
void execute
(Target target
);
void undo
();
void redo
();
String toString
();
}
/**
* Base class
for spell targets.
*/
public abstract class Target
{
private static final Logger LOGGER
= LoggerFactory.getLogger
(Target.class
);
private Size size
;
private Visibility visibility
;
public Size getSize
() {
return size
;
}
public void setSize
(Size size
) {
this.size
= size
;
}
public Visibility getVisibility
() {
return visibility
;
}
public void setVisibility
(Visibility visibility
) {
this.visibility
= visibility
;
}
@Override
public abstract String toString
();
/**
* Print status.
*/
public void printStatus
() {
LOGGER.info
("{}, [size={}] [visibility={}]", this, getSize
(), getVisibility
());
}
}
/**
* Enumeration
for target size.
*/
public enum Size
{
SMALL
("small"), NORMAL
("normal");
private final String title
;
Size
(String title
) {
this.title
= title
;
}
@Override
public String toString
() {
return title
;
}
}
/**
* Enumeration
for target visibility.
*/
public enum Visibility
{
VISIBLE
("visible"), INVISIBLE
("invisible");
private final String title
;
Visibility
(String title
) {
this.title
= title
;
}
@Override
public String toString
() {
return title
;
}
}
/**
* InvisibilitySpell is a concrete command.
*/
public class InvisibilitySpell implements Command
{
private Target target
;
@Override
public void execute
(Target target
) {
target.setVisibility
(Visibility.INVISIBLE
);
this.target
= target
;
}
@Override
public void undo
() {
if (target
!= null
) {
target.setVisibility
(Visibility.VISIBLE
);
}
}
@Override
public void redo
() {
if (target
!= null
) {
target.setVisibility
(Visibility.INVISIBLE
);
}
}
@Override
public String toString
() {
return "Invisibility spell(隐身术)";
}
}
/**
* ShrinkSpell is a concrete command.
*/
public class ShrinkSpell implements Command
{
private Size oldSize
;
private Target target
;
@Override
public void execute
(Target target
) {
oldSize
= target.getSize
();
target.setSize
(Size.SMALL
);
this.target
= target
;
}
@Override
public void undo
() {
if (oldSize
!= null
&& target
!= null
) {
var temp
= target.getSize
();
target.setSize
(oldSize
);
oldSize
= temp
;
}
}
@Override
public void redo
() {
undo
();
}
@Override
public String toString
() {
return "Shrink spell(收缩咒语)";
}
}
/**
* Wizard is the invoker of the commands.
*/
public class Wizard
{
private static final Logger LOGGER
= LoggerFactory.getLogger
(Wizard.class
);
private final Deque
<Command
> undoStack
= new LinkedList
<>();
private final Deque
<Command
> redoStack
= new LinkedList
<>();
public Wizard
() {
// comment to ignore sonar issue: LEVEL critical
}
/**
* Cast spell.
*/
public void castSpell
(Command command, Target target
) {
LOGGER.info
("{} casts {} at {}", this, command, target
);
command.execute
(target
);
undoStack.offerLast
(command
);
}
/**
* Undo last spell.
*/
public void undoLastSpell
() {
if (!undoStack.isEmpty
()) {
var previousSpell
= undoStack.pollLast
();
redoStack.offerLast
(previousSpell
);
LOGGER.info
("{} undoes {}", this, previousSpell
);
previousSpell.undo
();
}
}
/**
* Redo last spell.
*/
public void redoLastSpell
() {
if (!redoStack.isEmpty
()) {
var previousSpell
= redoStack.pollLast
();
undoStack.offerLast
(previousSpell
);
LOGGER.info
("{} redoes {}", this, previousSpell
);
previousSpell.redo
();
}
}
@Override
public String toString
() {
return "Wizard(巫师)";
}
}
/**
* Goblin is the target of the spells.
*/
public class Goblin extends Target
{
public Goblin
() {
setSize
(Size.NORMAL
);
setVisibility
(Visibility.VISIBLE
);
}
@Override
public String toString
() {
return "Goblin(小精灵)";
}
}