[英]Design Pattern recommendation
您可以尝试提取不同的行为以分离模块或函数,并根据您的运行时配置导入或调用适当的行为。 一个简单的方法是将 map 当前运行时的名称改为 function 名称,例如:
def show_contact_office1():
# display buttons and set up callbacks
def show_contact_office2():
# display some other buttons
contact_sections = {
'office1': show_contact_office1,
'office2': show_contact_office2,
}
def show_page(config):
current_office = config.get_office()
show_contact = contact_sections[current_office]
show_contact()
如果有许多这样的变量组件,您可以将它们包装到 class 中,默认实现作为父类,特定覆盖作为子类。 然后在您的代码中实例化适当的 class,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.