发布_订阅模式 实现

发布—订阅模式又叫观察者模式,它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知

不多说直接上代码,详细了解请自行百度~这里就不多说了

//发布-订阅模式
        function Observer() {
            let _map = new Map();
            function isFunction(fn) {
                if (!(fn instanceof Function)) {
                    throw 'arguments is not a function!'
                }
            }
            //发布对象
            this.add = function (prop) {
                let [calback, obj] = [arguments[2], arguments[1]];
                if (!calback) {
                    obj = {};
                    calback = arguments[1];
                }
                isFunction(calback);
                let array = _map.get(prop) || [];
                if (_map.has(prop)) {
                    array.push({
                        obj,
                        calback,
                    })
                } else {
                    array.push({
                        obj,
                        calback,
                    })
                    _map.set(prop, array);
                }
            }
            //订阅对象
            this.subscribe = function (props, ...args) {
                let result = _map.get(props) || [];
                result.forEach((val) => {
                    val.calback.apply(val.obj, args);
                })
            }

            //移除订阅对象
            this.remove = function (props) {
                let target = arguments[1];
                if (props) {
                    if (target) {
                        let result = _map.get("props");
                        result.forEach((val, index) => {
                            if (Object.is(target, val.obj)) {
                                result.splice(index, 1)
                            }
                        })
                    } else {
                        _map.delete('props');
                    }

                } else {
                    _map.clear()
                }
            }
        }

        let obs = new Observer();
        obs.add("emit", (a, b) => {
            console.log(this) //打印window
            console.log(a, b) //打印1,2
        })
        obs.add("emit", { a: 1 }, function () {
            console.log(this) //打印{a:1}
            console.log(arguments) //打印window
        })
        obs.subscribe('emit', 1, 2, 3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用IPC实现发布订阅模式的C语言代码示例: 1. 发布者代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <rtthread.h> #define MQ_NAME "mq_magic_test" #define MQ_SIZE 1024 static rt_mq_t mq_magic_pub = RT_NULL; int pub_thread_entry(void *parameter) { char msg[MQ_SIZE]; int i = 0; while (1) { sprintf(msg, "This is message %d from publisher.", i++); rt_mq_send(mq_magic_pub, msg, strlen(msg)); rt_kprintf("Publish message: %s\n", msg); rt_thread_mdelay(1000); } } int pub_init(void) { mq_magic_pub = rt_mq_create(MQ_NAME, MQ_SIZE, RT_IPC_FLAG_FIFO); if (mq_magic_pub == RT_NULL) { rt_kprintf("Failed to create message queue.\n"); return -1; } rt_thread_t pub_tid = rt_thread_create("pub", pub_thread_entry, RT_NULL, 1024, 25, 10); if (pub_tid != RT_NULL) { rt_thread_startup(pub_tid); } return 0; } MSH_CMD_EXPORT(pub_init, ipc pub/sub demo); ``` 2. 订阅者代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <rtthread.h> #define MQ_NAME "mq_magic_test" #define MQ_SIZE 1024 static rt_mq_t mq_magic_sub = RT_NULL; int sub_thread_entry(void *parameter) { char msg[MQ_SIZE]; while (1) { rt_memset(msg, 0, sizeof(msg)); rt_mq_recv(mq_magic_sub, msg, sizeof(msg), RT_WAITING_FOREVER); rt_kprintf("Receive message: %s\n", msg); } } int sub_init(void) { mq_magic_sub = rt_mq_create(MQ_NAME, MQ_SIZE, RT_IPC_FLAG_FIFO); if (mq_magic_sub == RT_NULL) { rt_kprintf("Failed to create message queue.\n"); return -1; } rt_thread_t sub_tid = rt_thread_create("sub", sub_thread_entry, RT_NULL, 1024, 25, 10); if (sub_tid != RT_NULL) { rt_thread_startup(sub_tid); } return 0; } MSH_CMD_EXPORT(sub_init, ipc pub/sub demo); ``` 以上代码中,发布者通过创建一个消息队列mq_magic_pub,并在一个线程中不断发送消息;订阅者通过创建一个同名的消息队列mq_magic_sub,并在一个线程中不断接收消息。这样就实现了一个简单的发布订阅模式

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值