很久没有写模式的文章了,今天发个
设计模式中的命令模式的。
模式概述
归类:操作性模式
目标:将一个请求封装为一个对象,从而使你不同的请求对客户进行参数化
实现方法:把请求封装在对象中,把请求调用变为对象的调用。可以想管理对象一样调用,在 时机和环境适合时进行调用。
特点:命令模式可以良好将请求进行封装,参数化,达到良好的底层封装,复用性,并且方便模块间的解耦,可对请求进行
序列化,便于日志处理,以及撤销重做等。
这个是我们所用的命令类的
接口,一系列命令都需要实现这个类。
package Command;
/**
* 命令
* @author ZERO
*/
public interface Command {
public void execute();
}
然后我们谁便写几个命令的实现类
package Command;
public class ConcreteCommand_A implements Command {
private final Receiver fReceiver;
public ConcreteCommand_A(Receiver receiver) {
super();
fReceiver = receiver;
}
public void execute() {
fReceiver.action_A();
}
}
package Command;
/**
* @author ZERO
*/
public class ConcreteCommand_B implements Command {
private final Receiver fReceiver;
public ConcreteCommand_B(Receiver receiver) {
super();
fReceiver = receiver;
}
public void execute() {
fReceiver.action_B();
}
}
package Command;
/**
* @author ZERO
*/
public class ConcreteCommand_C implements Command {
private final Receiver fReceiver;
public ConcreteCommand_C(Receiver receiver) {
super();
fReceiver = receiver;
}
public void execute() {
fReceiver.action_C();
}
}
这个是是命令的实现类。
然后就是我们的调用器了。
package Command;
/**
* 调用器
* @author ZERO
*/
public class Invoker {
private Command fCommand;
public Invoker() {
super();
}
public Invoker(Command cmd) {
super();
fCommand = cmd;
}
public void storeCommand(Command cmd) {
fCommand = cmd;
}
public void execute() {
fCommand.execute();
}
}
与调用器相对的就是我们的接收器了。这个可以对我们发出的命令进行实现。
package Command;
/**
* 接收的
*
* @author ZERO
*/
public class Receiver {
public Receiver() {
super();
}
public void action_A() {
System.out.println("Receiver_A");
}
public void action_B() {
System.out.println("Receiver_B");
}
public void action_C() {
System.out.println("Receiver_C");
}
}
最后我们来个测试类。
package Command;
import java.util.ArrayList;
import java.util.List;
/**
* @author ZERO
*/
public class Client_A {
private List<Invoker> invokers = new ArrayList<Invoker>();
/** stores the Receiver instance of the Client */
private final Receiver fReceiver;
/**
* This construtor creates a Client instance and stores the given Receiver.
*/
public Client_A(Receiver receiver) {
super();
fReceiver = receiver;
}
/**
* This method creates a ConcreteCommand instance and specifies a Receiver
* object.
*/
public void initConcreteCommand_A() {
ConcreteCommand_A cmd = new ConcreteCommand_A(fReceiver);
Invoker invoker = new Invoker();
invoker.storeCommand(cmd);
invokers.add(invoker);
}
/**
* This method creates a ConcreteCommand instance and specifies a Receiver
* object.
*/
public void initConcreteCommand_B() {
ConcreteCommand_B cmd = new ConcreteCommand_B(fReceiver);
Invoker invoker = new Invoker();
invoker.storeCommand(cmd);
invokers.add(invoker);
}
/**
* This method creates a ConcreteCommand instance and specifies a Receiver
* object.
*/
public void initConcreteCommand_C() {
ConcreteCommand_C cmd = new ConcreteCommand_C(fReceiver);
Invoker invoker = new Invoker();
invoker.storeCommand(cmd);
invokers.add(invoker);
}
public static void main(String[] args) {
Receiver receiver = new Receiver();
Client_A a = new Client_A(receiver);
a.initConcreteCommand_A();
a.initConcreteCommand_B();
a.initConcreteCommand_C();
for (Invoker invoker : a.invokers) {
invoker.execute();
}
}
}
运行的结果:
Receiver_A
Receiver_B
Receiver_C
命令模式在场景使用中很普遍,如果距离来说明的话。很对回合制RPG的命令模式就是这样的可以很好
理解的。模式的重点在于如何理解而不是疯狂的套用。