设计模式--命令模式

很久没有写模式的文章了,今天发个设计模式中的命令模式的。

模式概述
归类:操作性模式
目标:将一个请求封装为一个对象,从而使你不同的请求对客户进行参数化
实现方法:把请求封装在对象中,把请求调用变为对象的调用。可以想管理对象一样调用,在 时机和环境适合时进行调用。
特点:命令模式可以良好将请求进行封装,参数化,达到良好的底层封装,复用性,并且方便模块间的解耦,可对请求进行序列化,便于日志处理,以及撤销重做等。

这个是我们所用的命令类的接口,一系列命令都需要实现这个类。
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的命令模式就是这样的可以很好理解的。模式的重点在于如何理解而不是疯狂的套用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值