北京建设网站的公司兴田德润优惠,做视频网站视频源,个人做多个网站备案,国外的设计网站一、观察者模式
1.1 观察者模式定义
意图#xff1a; 定义对象间的一种一对多的依赖关系#xff0c;当一个对象的状态发生改变是#xff0c;所有依赖于它的对象都能得到通知并自动更新。
适用性#xff1a;
当一个对象状态的改变需要改变其他对象#xff0c; 或实际对… 一、观察者模式
1.1 观察者模式定义
意图 定义对象间的一种一对多的依赖关系当一个对象的状态发生改变是所有依赖于它的对象都能得到通知并自动更新。
适用性
当一个对象状态的改变需要改变其他对象 或实际对象是事先未知的或动态变化的时 可使用观察者模式。当应用中的一些对象对象观察其他对象时可以使用此模式。订阅列表是动态的因此订阅者可随时会加入或离开此列表。
1.2 观察者模式结构 发布者会向其他对象发送值得关注的事件。 事件会在发布者自身状态改变或执行特定行为后发生。 发布者中包含一个允许新订阅者加入和当前订阅者离开列表的订阅构架。 当新事件发生时 发送者会遍历订阅列表并调用每个订阅者对象的通知方法。 该方法是在订阅者接口中声明的。 订阅者 接口声明了通知接口。 在绝大多数情况下 该接口仅包含一个 update更新方法。 该方法可以拥有多个参数 使发布者能在更新时传递事件的详细信息。 具体订阅者 可以执行一些操作来回应发布者的通知。 所有具体订阅者类都实现了同样的接口 因此发布者不需要与具体类相耦合。 订阅者通常需要一些上下文信息来正确地处理更新。 因此 发布者通常会将一些上下文数据作为通知方法的参数进行传递。 发布者也可将自身作为参数进行传递 使订阅者直接获取所需的数据。 客户端 会分别创建发布者和订阅者对象 然后为订阅者注册发布者更新。
二、实例
实现思路
声明订阅者接口。 该接口声明一个通知方法。声明发布者接口并定义一些接口来在列表中添加和删除订阅对象。创建具体发布者类。 每次发布者发生了重要事件时都必须通知所有的订阅者。在具体订阅者类中实现通知更新的方法。 1.Monster
public class Monster : MonoBehaviour
{public string sname 怪物x;// Start is called before the first frame updatevoid Start(){Invoke(Dead,1);}void Dead(){Debug.Log(怪物死亡);//GameObject.Find(Player).GetComponentPlayer().MonsterDead(this);//GameObject.Find(Task).GetComponentTask().TaskmonsterDead(this);//GameObject.Find(Other).GetComponentOther().OthermonsterDead(this);EventCenter.GetInstance().EventTrigger(MonsterDead,this);}
}
2.Player
void Start()
{ EventCenter.GetInstance().AddEventListener(MonsterDead, MonsterDeadDo);
}public void MonsterDeadDo(object info)
{Debug.Log(玩家的奖励 (info as Monster).name);
}void OnDestory()
{EventCenter.GetInstance().RemoveEventListener(MonsterDead, MonsterDeadDo);
}
3.EventCenter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;/// summary
/// 事件中心 单例模式对象
/// 1.dictionary
/// 2.委托
/// 3.观察者设计模式
/// /summary
public class EventCenter : SingletonEventCenter
{//使用泛型委托传递怪物-蝙蝠等 具体敌人类型public Dictionarystring, UnityActionobject eventDic new Dictionarystring, UnityActionobject();/// summary/// 添加事件监听/// /summary/// param namename事件的名字/param/// param nameaction准备用来处理事件的 委托函数/parampublic void AddEventListener(string name,UnityActionobject action){//判断事件中心是否有此 函数if (eventDic.ContainsKey(name))eventDic[name] action;//无elseeventDic.Add(name, action);}/// summary/// 执行函数/// /summary/// param namename事件名/param/// param nameinfo传递参数 具体怪物类型/parampublic void EventTrigger(string name,object info){//判断触发中心是否有此 函数if (eventDic.ContainsKey(name)){eventDic[name](info);}}/// summary/// 移除对应事件监听/// /summary/// param namename事件名/param/// param nameaction委托函数/parampublic void RemoveEventListener(string name, UnityActionobject action){//判断事件中心是否有此 函数if (eventDic.ContainsKey(name))eventDic[name] - action;}/// summary/// 清空事件中心主要用于场景切换/// /summarypublic void Clear(){eventDic.Clear();}
}
文章转载自: http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.fysdt.cn.gov.cn.fysdt.cn http://www.morning.fqlxg.cn.gov.cn.fqlxg.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.gqwpl.cn.gov.cn.gqwpl.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.rksg.cn.gov.cn.rksg.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.mqfkd.cn.gov.cn.mqfkd.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.tygn.cn.gov.cn.tygn.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.jqcrf.cn.gov.cn.jqcrf.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.clwhf.cn.gov.cn.clwhf.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.lgtcg.cn.gov.cn.lgtcg.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.hxxwq.cn.gov.cn.hxxwq.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.btpll.cn.gov.cn.btpll.cn