場景是這樣的在commands目錄中會有一堆的py文件。每個文件都是一個class。每個class都有不同的方法。比如#user.pyclass user():
def login():
pass# order.pyclass order():
def close(order_id):
pass像這種文件在commands目錄中有很多,而且隨時會增加或者減少。有一個daemon.py會根據MQ消息隊列的命令動態的實例化某個命令并且傳遞對應的參數過去。# daemon.py 偽代碼import commandswhile True:
message = json.loads(mq.receive()) if not hasattr(commands, message['class'])
logger.warn("error")
command = commands[message['class']]()
command. message['action'] (message) ????應該如何實現或者我不應該這么用python?用php用慣了嗎?
如何更優雅的實現工廠模式或其他解決方法
幕布斯6054654
2018-09-08 16:36:56