• 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe


    Python版

    https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    Reference:
    http://www.slideshare.net/ishraqabd/publish-subscribe-model-overview-13368808
    Author: https://github.com/HanWenfang
    """
    
    
    class Provider:
    
        def __init__(self):
            self.msg_queue = []
            self.subscribers = {}
    
        def notify(self, msg):
            self.msg_queue.append(msg)
    
        def subscribe(self, msg, subscriber):
            self.subscribers.setdefault(msg, []).append(subscriber)
    
        def unsubscribe(self, msg, subscriber):
            self.subscribers[msg].remove(subscriber)
    
        def update(self):
            for msg in self.msg_queue:
                for sub in self.subscribers.get(msg, []):
                    sub.run(msg)
            self.msg_queue = []
    
    
    class Publisher:
    
        def __init__(self, msg_center):
            self.provider = msg_center
    
        def publish(self, msg):
            self.provider.notify(msg)
    
    
    class Subscriber:
    
        def __init__(self, name, msg_center):
            self.name = name
            self.provider = msg_center
    
        def subscribe(self, msg):
            self.provider.subscribe(msg, self)
    
        def unsubscribe(self, msg):
            self.provider.unsubscribe(msg, self)
    
        def run(self, msg):
            print("{} got {}".format(self.name, msg))
    
    
    def main():
        message_center = Provider()
    
        fftv = Publisher(message_center)
    
        jim = Subscriber("jim", message_center)
        jim.subscribe("cartoon")
        jack = Subscriber("jack", message_center)
        jack.subscribe("music")
        gee = Subscriber("gee", message_center)
        gee.subscribe("movie")
        vani = Subscriber("vani", message_center)
        vani.subscribe("movie")
        vani.unsubscribe("movie")
    
        fftv.publish("cartoon")
        fftv.publish("music")
        fftv.publish("ads")
        fftv.publish("movie")
        fftv.publish("cartoon")
        fftv.publish("cartoon")
        fftv.publish("movie")
        fftv.publish("blank")
    
        message_center.update()
    
    
    if __name__ == "__main__":
        main()
    
    ### OUTPUT ###
    # jim got cartoon
    # jack got music
    # gee got movie
    # jim got cartoon
    # jim got cartoon
    # gee got movie
    Python转载版
  • 相关阅读:
    安装 android sdk 时,dl.google.com 连不上各种尝试
    解决android SDK 安装过程中 packages 列表为空的问题
    Java 集合 -- Deque
    Java 集合 -- Queue
    Java 集合 -- Set
    Java 集合 -- Map
    Java 集合 -- List
    Java 语言进阶 -- 线程
    Java 语言基础知识
    Java 网络编程基础 -- TCP 编程
  • 原文地址:https://www.cnblogs.com/demonzk/p/9035652.html
Copyright © 2020-2023  润新知