unity设计模式——观察者模式

观察者模式委托原型:

在委托中,通过字典添加或者移除监听

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 观察者模式
/// </summary>
public class EventDispather : Singleton<EventDispather>
{
    //委托原型
    public delegate void OnActionHandler(params object[] parmas);
    //委托字典
    private Dictionary<int,List<OnActionHandler>> dic = new Dictionary<int, List<OnActionHandler>>();

    /// <summary>
    /// 添加监听
    /// </summary>
    /// <param name="id"></param>
    /// <param name="handler"></param>
    public void AddEventListenner(int id, OnActionHandler handler)
    {
        if (dic.ContainsKey(id))
        {
            dic[id].Add(handler);
        }
        else
        {
            List<OnActionHandler> ishandle = new List<OnActionHandler> ();
            ishandle.Add(handler);
            dic[id] = ishandle;
        }
    }

    /// <summary>
    /// 移除监听
    /// </summary>
    /// <param name="id"></param>
    /// <param name="handler"></param>
    public void RemoveEventListenner(int id, OnActionHandler handler)
    {
        if(dic.ContainsKey(id))
        {
            List<OnActionHandler > ishandle = dic[id];
            ishandle.Remove(handler);
            if(ishandle.Count == 0)
            {
                dic.Remove(id);
            }
        }
    }

    /// <summary>
    /// 派发消息
    /// </summary>
    /// <param name="id"></param>
    /// <param name="value"></param>
    public void DisPath(int id, params object[] parmas)
    {
        if(dic.ContainsKey(id) )
        {
            List<OnActionHandler> ishandler = dic[id];
            if(ishandler.Count > 0 && ishandler != null)
            {
                for (int i = 0; i < ishandler.Count; i++)
                {
                    if (ishandler[i] != null)
                    {
                        ishandler[i](parmas);
                    }
                }
            }
        }
    }
}

消息发送:

/// <summary>
/// 消息发送者
/// </summary>
public class EventSend : MonoBehaviour
{
  
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.A))
        {
            EventDispather.instance.DisPath(0, 9);
        }
        else if (Input.GetKeyUp(KeyCode.B))
        {
            EventDispather.instance.DisPath(1, 18);
        }
    }
}

观察者监听:

观察者A:

/// <summary>
/// 消息监听A,监听2个消息
/// </summary>
public class EventGetA : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        EventDispather.instance.AddEventListenner(0, Masege0);
        EventDispather.instance.AddEventListenner(1, Masege1);

    }

    private void Masege1(object[] parmas)
    {
        Debug.Log((int)parmas[0] + "点下班");
    }

    private void Masege0(object[] parmas)
    {
        Debug.Log((int)parmas[0] + "点上班");
    }

    private void OnDestroy()
    {
        EventDispather.instance.RemoveEventListenner(0, Masege0);
        EventDispather.instance.RemoveEventListenner(1, Masege1);

    }
}

观察者B:

/// <summary>
/// 消息监听B,只监听其中一个消息
/// </summary>
public class EventGetB : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        EventDispather.instance.AddEventListenner(1, Masege1);
    }

    private void Masege1(object[] parmas)
    {
        Debug.Log((int)parmas[0] + "点下班");
    }

    private void OnDestroy()
    {
        EventDispather.instance.RemoveEventListenner(1, Masege1);

    }
}

优点:

- 观察者模式可以避免普通委托不必要的消息接受

- 降低了目标与观察者之间的耦合关系,两者之间是抽象耦合关系。符合依赖倒置原则。

- 目标与观察者之间建立了一套触发机制。

缺点:

- 目标与观察者之间的依赖关系并没有完全解除,而且有可能出现循环引用。

- 当观察者对象很多时,通知的发布会花费很多时间,影响程序的效率。

猜你喜欢

转载自blog.csdn.net/qq_29296473/article/details/131402255