设计模式学习-命令模式

概念

命令,接收者,执行者,一个命令模式由这些基本的组件组成。

接收者 会有一个函数

命令有一个持有接收者并且 有一个 执行函数

执行者 持有一个命令 并且 会执行这个命令

csharp 复制代码
using UnityEngine;
using System.Collections;
namespace CommondStructure{
public class CommandStructre: MonoBehaviour{
	Receiver receiver = new Receiver();
	ConcreteCommand command = new ConcreteCommand(receiver);
	Invoker invoker = new Invoker();
	
	invoker.SetCommand(command);
	invoker.InvokeCommand ();
}

abstract class Command{
	protected Receiver receiver;
	public Command(Receiver receiver){
		this.receiver = receiver;
	}
	public abstract void Execute();
}
public class ConcreteCommand:Command{
	public ConcreteCommand(Receiver receiver):base(receiver){}
	public override void Execute(){
		receiver.Action();
	}
}

class Receiver{
	public void Action(){
		Debug.Log("我被执行了");
	}
}	
class Invoker{
	private Command_command;
	public void SetCommond(Command command){
		this._command= command;
	}
	public void InvokeCommand (){
		_command.Execute();
	}
}
}

主旨:接收者 是存在需要执行的函数,命令只负责进行桥接

执行者 根据命令执行接收到到命令的接收者的方法

相关推荐
HarmonSir20 小时前
Codex 架构解析:一条对话如何长成一个运行时系统
后端·设计模式·架构
淡海水1 天前
01-03-认知篇-基础-Excel配置工作流痛点深度分析
unity·架构·excel·游戏开发·luban
鱼很腾apoc1 天前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
楚山之石2 天前
【Lancet】BRAID 随机试验:乳腺致密女性补充成像,abbreviated MRI 与 CEM 检出更多早期癌(首轮中期结果)
人工智能·科技·学习·mri·医学影像
饮水桶_T2 天前
Java调用大语言模型 笔记
笔记·设计模式·电脑·ai编程
Sunflower_ac2 天前
强化学习学习笔记:Lab 0–Lab 2
笔记·学习
Z21-X2 天前
云原生运维学习笔记:Kubernetes 自动扩缩容与资源管控实战
运维·笔记·学习·云原生·kubernetes
可涵不会debug2 天前
ChatServer 模块学习笔记:HTTP 接入层、多线程与流式响应
笔记·学习·http
console.log('npc')3 天前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
ssshooter3 天前
Replit 项目本地跑:一份实测过的改造清单
前端·后端·设计模式