设计模式学习-命令模式

概念

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

接收者 会有一个函数

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

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

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

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

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

相关推荐
三只坚果1 小时前
blender制作动画导入unity两种方式
unity·游戏引擎·blender
好望角雾眠1 小时前
第一阶段C#基础-10:集合(Arraylist,list,Dictionary等)
笔记·学习·c#
艾伦~耶格尔1 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
星仔编程2 小时前
python学习DAY46打卡
学习
benben0442 小时前
《Unity Shader入门精要》学习笔记二
unity·unity shader
大霞上仙2 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
YF云飞3 小时前
Unity音频管理:打造沉浸式游戏音效
游戏·unity·游戏引擎·游戏程序·个人开发
yatingliu20194 小时前
HiveQL | 个人学习笔记
hive·笔记·sql·学习
武当豆豆4 小时前
C++编程学习(第25天)
开发语言·c++·学习
风和日丽 随波逐流4 小时前
java17学习笔记-Deprecate the Applet API for Removal
笔记·学习