我意识到标题很糟糕,请随意更改或提出更改建议(我对设计图案不熟悉)
我有一个基于Python的Dash web应用程序(不完全相关),我想给它添加一些结构,但是我不确定使用哪种设计模式
我尝试的结构是:
-- Parent object (initializes all the configuration and instantiates a data object that the rest of the children will need to use)
|
|- Have child objects with methods that return the layout, which I want to be able to call from the parent object
但是,我意识到这可能不是最好的方法,因为很难从父对象调用子方法
以下是一个例子:
class Parent:
def __init__(self):
self.data = VaccinationData() <-- created parent so I can use this data in entire project
self.app = dash.Dash()
self.set_layout()
def set_layout(self):
self.app.layout = html.Div(self.child_method()) <-- calling child method
class Child(Parent):
def child_method(self):
# Does some data processing with self.data
return html.Div(
className="right",
children=[self.top_stats(), self.pred_full_vacc()], <-- calling more child's child methods
)
if __name__ == "__main__":
app = Parent()
app.app.run_server(debug=True)
我对设计模式还不太熟悉,因此非常感谢您的帮助强>
我知道您试图在共存类之间创建链接,并且父类和子类实际上与父类及其子类相关(因为有一个疫苗接种数据声明)。 在这种情况下,您可以创建一个父类(Person)和两个子类(parent和child)。 因此,您可以组织代码,将两个子类都需要访问的方法保留在父类上。 尝试通过编码链接这些类(比如在子类的init方法上实例化需要输入父实例的变量)
相关问题 更多 >
编程相关推荐