设计模式学习-命令模式

概念

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

接收者 会有一个函数

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

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

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();
	}
}
}

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

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

相关推荐
AI成长日志2 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
_李小白3 小时前
【OSG学习笔记】Day 38: TextureVisitor(纹理访问器)
android·笔记·学习
杨云龙UP4 小时前
从0到1快速学会Linux操作系统(基础),这一篇就够了!
linux·运维·服务器·学习·ubuntu·centos·ssh
头疼的程序员4 小时前
计算机网络:自顶向下方法(第七版)第八章 学习分享(三)
网络·学习·计算机网络
_李小白5 小时前
【OSG学习笔记】Day 37: NodeVisitor(顶点访问器)
笔记·学习
程序员雷欧5 小时前
大模型应用开发学习第八天
大数据·人工智能·学习
RReality5 小时前
【Unity Shader URP】序列帧动画(Sprite Sheet)实战教程
unity·游戏引擎
mxwin5 小时前
Unity URP 多线程渲染:理解 Shader 变体对加载时间的影响
unity·游戏引擎·shader
晓晓hh6 小时前
JavaSE学习——set集合和Map映射
学习
لا معنى له6 小时前
Var-JEPA:联合嵌入预测架构的变分形式 —— 连接预测式与生成式自监督学习 ----论文翻译
人工智能·笔记·学习·语言模型