数据驱动模式
数据驱动模式(Data-Driven Design Pattern)是一种非常实用、易于扩展的设计思想,在现代软件开发中应用非常广泛,尤其适用于游戏开发、业务规则系统、UI 构建、工作流引擎等场景。
什么是数据驱动模式?
数据驱动模式是一种将程序的行为或配置从代码中剥离出来,用数据来控制程序逻辑的设计方式。也就是说,程序本身的逻辑结构相对固定,而执行细节通过外部数据进行“注入”或“控制”。
通俗地说,就是:“程序少写点,数据多干活”。
核心思想 🌟
把逻辑从硬编码中抽离出来,改为通过数据(配置、规则、模型等)来驱动行为。
数据驱动模型具有以下特点:
- 把 控制流程、业务逻辑、状态机、行为规则等 设计为可配置的数据。
- 程序框架本身只负责 读取数据并执行相应操作。
- 更易于扩展、修改和调试。
示例:菜单命令系统
下面我们来设计一个数据驱动的“菜单命令系统”。这个系统模拟了当你运行一个应用时,点击菜单项触发相应功能的行为。我们将把菜单项和对应的处理逻辑都放到数据里,而不是硬编码写在程序中。
我们希望支持这样的功能:
- 菜单项通过数据描述;
- 每个菜单项绑定一个命令名称;
- 用户输入命令时自动执行绑定函数;
- 新增菜单项时不需要改程序,只需添加数据。
菜单数据(JSON 格式)
[
{ "label": "Say Hello", "command": "hello" },
{ "label": "Show Version", "command": "version" },
{ "label": "Exit", "command": "exit" }
]
Python 实现
import json
# 菜单配置
menu_config = '''
[
{ "label": "Say Hello", "command": "hello" },
{ "label": "Show Version", "command": "version" },
{ "label": "Exit", "command": "exit" }
]
'''
# 命令映射表
def say_hello():
print("Hello, World!")
def show_version():
print("App version: 1.0.0")
def exit_app():
print("Exiting...")
exit(0)
command_table = {
"hello": say_hello,
"version": show_version,
"exit": exit_app
}
# 主逻辑
menu = json.loads(menu_config)
while True:
print("\n=== Menu ===")
for i, item in enumerate(menu, 1):
print(f"{i}. {item['label']}")
choice = input("Select an option (number): ").strip()
if not choice.isdigit():
continue
idx = int(choice) - 1
if 0 <= idx < len(menu):
cmd = menu[idx]["command"]
handler = command_table.get(cmd)
if handler:
handler()
else:
print(f"Unknown command: {cmd}")