場(chǎng)景是這樣的在commands目錄中會(huì)有一堆的py文件。每個(gè)文件都是一個(gè)class。每個(gè)class都有不同的方法。比如#user.pyclass user():
def login():
pass# order.pyclass order():
def close(order_id):
pass像這種文件在commands目錄中有很多,而且隨時(shí)會(huì)增加或者減少。有一個(gè)daemon.py會(huì)根據(jù)MQ消息隊(duì)列的命令動(dòng)態(tài)的實(shí)例化某個(gè)命令并且傳遞對(duì)應(yīng)的參數(shù)過去。# 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) ????應(yīng)該如何實(shí)現(xiàn)或者我不應(yīng)該這么用python?用php用慣了嗎?
如何更優(yōu)雅的實(shí)現(xiàn)工廠模式或其他解決方法
幕布斯6054654
2018-09-08 16:36:56