设计模式学习-命令模式

概念

命令,接收者,执行者,一个命令模式由这些基本的组件组成。
接收者 会有一个函数
命令有一个持有接收者并且 有一个 执行函数
执行者 持有一个命令 并且 会执行这个命令

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

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值