设计模式系列(十五)命令模式

参考:

https://www.cnblogs.com/java-my-life/archive/2012/06/01/2526972.html

https://www.cnblogs.com/zuoxiaolong/p/pattern13.html

一:基本理论

命令模式属于对象的行为模式。命令模式又称为行动(Action)模式或交易(Transaction)模式。

命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。

命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。

每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。

 

命令模式包含如下角色:

  • Command: 抽象命令类
  • ConcreteCommand: 具体命令类
  • Invoker: 调用者
  • Receiver: 接收者
  • Client:客户类

../_images/Command.jpg

代码:

/**
 * Created by Administrator on 2019/6/12.
 */
public interface Command {
    void execute();
}
/**
 * Created by Administrator on 2019/6/12.
 */
public class Command1 implements Command {
    private CustomReveiver receiver;

    public Command1(CustomReveiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void execute() {
        receiver.Handle();
    }
}


/**
 * Created by Administrator on 2019/6/12.
 */
public class CustomReveiver {
    public void Handle(){}
}
/**
 * Created by Administrator on 2019/6/12.
 */
public class Invoker {
    private Command command;

    public void SetCommand(Command command){
        this.command = command;
    }

    public void action() {
        this.command.execute();
    }
}
/**
 * Created by Administrator on 2019/6/12.
 */
public class Main {
    public static void main(String args[]){
        Invoker invoker = new Invoker();
        CustomReveiver receiver = new CustomReveiver();
        Command command1 = new Command1(receiver);
        invoker.SetCommand(command1);
        invoker.action();
    }
}

命令模式的核心角色:调用者,接受者,命令

 

二 现实例子

1.handler,looper,Messagequeue机制

调用者 Looper

接受者 Handler

命令  Message

2.线程例子

Threadstart()方法即命令的调用者,同时Thread的内部会调用Runnablerun(),这里Thread又充当了具体的命令角色,最后的Runnable则是接受者了,负责最后的功能处理。

3.Activity过程中的命令模式

在Android9.0以后,activity启动过程的通行机制有所修改,通行对象都变成了ClientTransactionItem 

在这里,一个个ClientTransactionItem就是Message或者说Command

  @Override
    public void execute(ClientTransactionHandler client, IBinder token,
            PendingTransactionActions pendingActions) {
        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
        ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo,
                mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState,
                mPendingResults, mPendingNewIntents, mIsForward,
                mProfilerInfo, client);
        client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
        Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
    }

Invoker是TransactionExecutor

  /**
     * Execute transaction immediately without scheduling it. This is used for local requests, so
     * it will also recycle the transaction.
     */
    @VisibleForTesting
    public void executeTransaction(ClientTransaction transaction) {
        transaction.preExecute(this);
        getTransactionExecutor().execute(transaction);
        transaction.recycle();
    }

接收者是ActivityThread,因为最后执行还是Activity做perform,resume这些操作

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值