zoukankan      html  css  js  c++  java
  • 插拔式设计思想

    插拔式设计思想

    notify文件夹下:

    __init__.py:

    关键代码(结合了 importlib 动态导入、反射 等知识点)

    import settings
    import importlib
    
    def send_all(content):
        for module_path in settings.NOTIFY_LIST:
            module, class_name = module_path.rsplit('.', maxslipt=1)
            # module = 'notify.email'  class_name = 'Email'
            mod = importlib.import_module(module)  # mod时模块名,例如email.py
            cls = getattr(mod, class_name)
            obj = cls()
            obj.send(content)
    

    email.py:

    class Email(object):
        def __init__(self):
            pass   # 省略相关配置操作
        
        def send(self, content):
            print('邮件通知:%s' % content)
    

    msg.py:

    class Msg(object):
        def __init__(self):
            pass  # 省略相关配置操作
    
        def send(self,content):
            print('短信通知:%s'%content)
    

    wechat:

    class WeChat(object):
        def __init__(self):
            pass  # 省略相关配置操作
    
        def send(self, content):
            print('微信通知:%s' % content)
    

    qq.py:

    class QQ(object):
        def __init__(self):
            pass   # 省略相关配置操作
    
        def send(self,content):
            print('qq通知:%s'%content)
    

    settings.py

    可以在这里开启或关闭功能

    NOTIFY_LIST = [
        'notify.email.Email',
        # 'notify.msg.Msg',   # 注销掉msg,可以关闭短信发送消息的功能
        'notify.wechat.WeChat',
        'notify.qq.QQ',
    ]
    

    运行入口

    notify文件夹之外/run.py

    import notify
    
    notify.send_all('测试信息')
    
  • 相关阅读:
    ZOJ3513_Human or Pig
    ZOJ2083_Win the Game
    ZOJ2725_Digital Deletions
    ZOJ2686_Cycle Gameu
    UVALive
    ZOJ2290_Game
    ZOJ3067_Nim
    P3159 [CQOI2012]交换棋子(费用流)
    P3153 [CQOI2009]跳舞(最大流多重匹配)
    P3121 [USACO15FEB]审查(黄金)Censoring (Gold)(ac自动机)
  • 原文地址:https://www.cnblogs.com/demiao/p/11795061.html
Copyright © 2011-2022 走看看