封装好的观察者模式

public class PengYouquan
{
    #region 单例

    private static PengYouquan instance;

    public static PengYouquan Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new PengYouquan();
            }

            return instance;
        }
    }
    PengYouquan() { }

    #endregion




    public delegate void OnActinHandler(int time);
    public static  OnActinHandler delegate_00;
    

    //委托字典
    Dictionary<string , List<OnActinHandler>> dic = new Dictionary<string , List<OnActinHandler>>();

    /// <summary>
    /// 添加监听 [给监听者使用,想监听了就添加,不需要监听的就不用管]
    /// </summary>
    /// <param name="protoID"></param>
    /// <param name="handler"></param>
    public void AddEventListener(string  protoID, OnActinHandler handler)
    {
        if (dic.ContainsKey(protoID))
        {
            dic[protoID].Add(handler);
        }
        else
        {
            List<OnActinHandler> lsHandler = new List<OnActinHandler>();
            lsHandler.Add(handler);
            dic.Add(protoID, lsHandler);
            //dic[protoID] = lsHandler;
        }
    }
    /// <summary>
    /// 移除监听  [给监听者使用,如果有监听某条消息必须有移除,否则死人还能收到消息]
    /// </summary>
    /// <param name="protoID"></param>
    /// <param name="handler"></param>
    public void RemoveEventListener(string  protoID, OnActinHandler handler)
    {
        if (dic.ContainsKey(protoID))
        {
            List<OnActinHandler> lsHandler = dic[protoID];
            lsHandler.Remove(handler);
            if (lsHandler.Count == 0)
            {
                dic.Remove(protoID);
            }
        }

    }
    /// <summary>
    /// 派发消息 【给发布者使用,不监听的人我就不发给他,谁监听谁能收到】
    /// </summary>
    /// <param name="protoID"></param>
    /// <param name="arg"></param>
    public void Dispatch(string  protoID, int time)
    {
        if (dic.ContainsKey(protoID))
        {//先根据id将list拿到
            List<OnActinHandler> lsHandler = dic[protoID];
            if (lsHandler != null && lsHandler.Count > 0)
            {
                for (int i = 0; i < lsHandler.Count; i++)
                {
                    //判断空执行
                    if (lsHandler[i] != null)
                    {
                        lsHandler[i](time);
                    }
                }
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,观察者模式被广泛应用于事件驱动的编程模型。Spring Boot通过使用事件和监听器来实现观察者模式,以实现对象之间的解耦和通信。 在Spring Boot中,事件是一个特殊的对象,它封装了与应用程序相关的状态信息。当某个特定的事件发生时,Spring Boot会自动通知所有注册的监听器,并将事件对象传递给它们。监听器可以根据事件的类型和内容来执行相应的操作。 下面是一个简单的示例,演示了如何在Spring Boot中使用观察者模式: 1. 创建一个事件类,例如`MyEvent`,用于封装事件的相关信息。 2. 创建一个监听器类,例如`MyEventListener`,实现`ApplicationListener`接口,并指定要监听的事件类型。 ```java import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class MyEventListener implements ApplicationListener<MyEvent> { @Override public void onApplicationEvent(MyEvent event) { // 处理事件 System.out.println("Received event: " + event.getMessage()); } } ``` 3. 在需要触发事件的地方,使用`ApplicationEventPublisher`接口的实现类来发布事件。 ```java import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @Component public class MyEventPublisher { private final ApplicationEventPublisher eventPublisher; public MyEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } public void publishEvent(String message) { // 创建事件对象 MyEvent event = new MyEvent(message); // 发布事件 eventPublisher.publishEvent(event); } } ``` 通过以上步骤,我们就可以在Spring Boot中使用观察者模式了。当`MyEventPublisher`发布事件时,`MyEventListener`会自动接收到事件并执行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值