﻿# 14. Command 拦截

QFramework 提供了拦截 Command 的 API。

我们尝试在 CounterApp 中实现一个 Command 日志。

代码很简单，如下:

```csharp
public class CounterApp : Architecture<CounterApp>
{
    protected override void Init()
    {
        // 注册 System 
        this.RegisterSystem<IAchievementSystem>(new AchievementSystem()); 
             
        // 注册 Model
        this.RegisterModel<ICounterAppModel>(new CounterAppModel());
            
        // 注册存储工具的对象
        this.RegisterUtility<IStorage>(new Storage());
    }

    protected override void ExecuteCommand(ICommand command)
    {
        Debug.Log("Before " + command.GetType().Name + "Execute");
        base.ExecuteCommand(command);
        Debug.Log("After " + command.GetType().Name + "Execute");
    }
}
```

只需要在 Architecture 中覆写 ExecuteCommand 即可。

运行之后，笔者随意点击了几次按钮，结果如下:

![image.png](https://file.liangxiegame.com/96bdc2f4-222d-4e91-a10e-dc2128e50fb4.png)

这样就实现了一个非常简单的 Command 日志功能。


## 有了 Command 拦截有什么用？

有了 Command 拦截功能，我们可以做非常多的事情，比如：
* Command 日志可以用来方便调试
* 可以实现 Command 中间件模式 可以写各种各样额度 Command 中间件，比如 Command 日志中间件
* 可以方便你先撤销功能
* 可以用 Command 做自动化测试
* 等等

好了这篇就介绍到这里。


## 更多内容

*   转载请注明地址：[liangxiegame.com](https://liangxiegame.com) （首发） 微信公众号：凉鞋的笔记
*   QFramework 主页：[qframework.cn](https://qframework.cn)
*   QFramework 交流群: 623597263
*   QFramework Github 地址: [https://github.com/liangxiegame/qframework](https://github.com/liangxiegame/qframework)
*   QFramework Gitee 地址：[https://gitee.com/liangxiegame/QFramework](https://gitee.com/liangxiegame/QFramework)
*   GamePix 独立游戏学院 & Unity 进阶小班地址：[https://www.gamepixedu.com/](https://www.gamepixedu.com/)