命令模式

《Android设计模式》读书笔记,便于记忆与以后查看,如有侵权,请尽快联系我删除,谢谢



一。定义
   我们常接触比较多的命令模式例子无非就是程序菜单命令,如在操作系统中,点击关机命令,系统就会做出一系列的操作,如暂停处理事件,保存系统的一些配置,然后结束程序进程,最后调用内核命令关闭计算机等。对于一系列的命令,用户不用去管,只需点击系统的关机按钮就可完成上述一系列的指令。而命令模式与之相同,将一系列的方法调用封装,用户只需调用一个方法执行,那么所有这些被封装的方法就会被挨个执行调用。
   将一个请求封装城一个对象,从而让用户使用不同的请求吧客户端参数化;对请求排队或记录请求日志,以及支持可撤销的操作
二。使用场景
(1)需要抽象出待执行的动作,然后以参数的形式提供出来——类似于过程设计中的回调机制,而命令模式正是回调机制的一个面向对象的替代品。
(2)在不同的时刻执行,排列和执行请求,一个命令对象可以有与初始请求无关的生存期
(3)需要支持取消操作
(4)支持修改日志功能,这样当系统崩溃时,这些修改可以被重做一次
(5)需要支持事务操作
三。代码实现
下面实现命令模式的通用模式代码,以及说明
*****************************************************************************************
package com.yinazh.designpattern;

//接受者类:负责具体实施或执行一个请求,执行具体逻辑的角色
public class Receiver{
    //真正执行具体命令逻辑的方法
public void action(){
System.out.println("执行具体的操作");
}
}

//抽象命令接口:所有具体命令的抽象接口
public interface Command{
void execute();
}

//具体命令类:在execute()方法中,调用接受者角色的相关方法,在接受者与命令执行的具体行为之间加以弱耦合
//execute()称为执行方法,具体可以包含一系列的逻辑处理的接受者方法
public class ConcreteCommand implements Command{
private Receiver receiver;//持有一个对接受者对象的引用

public ConcreteCommand(Receiver receiver){
this.receiver = receiver;
}

public void execute(){
    //调用接受者的相关方法来执行具体的逻辑
receiver.action();
}
}

//请求者类:调用命令对象执行具体的请求
public class Invoker{
private Command command;//持有一个对相应命令的引用
public Invoker(Command command){
this.command = command;
}
public void action(){
    //调用具体命令对象的相关方法,执行具体命令
command.execute();
}
}

//客户类:
public class Client{
Receiver receiver = new Receiver();
Command command = new ConcreteCommand(receiver);
Invoker invoker = new Invoker(command);
invoker.action();
}
*****************************************************************************************
  将行为调用者与实现者解藕。下面实现一个具体的例子,俄罗斯方块游戏的例子。
*****************************************************************************************
package com.yinazh.designpattern;

public class TetrisMachine{
public void toLeft(){
System.out.println("to left");
}
public void toRight(){
System.out.println("to right");
}
public void fastToBottom(){
System.out.println("fast to bottom");
}
public void transform(){
System.out.println("exchange shape");
}
}

public interface Command{
void execute();
}

public class LeftCommand implements Command{
private TetrisMachine machine;
public LeftCommand(TetrisMachine machine){
this.machine = machine;
}
public execute(){
machine.toLeft();
}
}

public class RightCommand implements Command{
private TetrisMachine machine;
public RightCommand(TetrisMachine machine){
this.machine = machine;
}
public execute(){
machine.toRight();
}
}

public class FallCommand implements Command{
private TetrisMachine machine;
public FallCommand(TetrisMachine machine){
this.machine = machine;
}
public execute(){
machine.fastToBottom();
}
}

public class TransformCommand implements Command{
private TetrisMachine machine;
public TransformCommand(TetrisMachine machine){
this.machine = machine;
}
public execute(){
machine.Transform();
}
}

public class Buttons{
private LeftCommand leftCommand;
private RightCOmmand rightCommand;
private FallCommand fallCommand;
private TransformCommand transformCommand;

public void setLeftCommand(LeftCommand lc){
this.leftCommand = lc;
}
public void setRightCommand(RightCommand rc){
this.rightCommand = rc;
}
public vodi setFallCommand(FallCommand fc){
this.fallCommand = fc;
}
public void setTransformCOmmand(TransformCommand tc){
this.transformCommand = tc;
}

public void toLeft(){
leftCommand.execute();
}
public void toRight(){
rightCommand.execute();
}
public void fall(){
fallCommand.execute();
}
public void transform(){
transformCommand.execute();
}
}

public class Player{
public static void main(String[] args){
TetrisMachine machine = new TetrisMachine();

LeftCommand leftCommand = new LeftCommand(machine);
RightCommand rightCommand = new RightCommand(machine);
FallCommand fallCommand = new FallCommand(machine);
TransformCommand transformCommand = new TransformCommand(machine);

Buttons buttons = new Buttons();
buttons.setLeftCommand(leftCommand);
buttons.setRightCommand(rightCommand);
buttons.setFallCommand(fallCommand);
buttons.setTransformCOmmand(transformCommand);

buttons.toLeft();
buttons.toRight();
buttons.fall();
buttons.transform();
}
}
*****************************************************************************************
  上述实现看着逻辑复杂,但实现了对修改关闭对扩展开放的原则。同时使用命令模式还可以实现命令记录的功能。
四。总结
  命令模式也有其他设计模式的通病,类的膨胀,大量衍生类的创建,这是不可避免的。











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值