So I'm currently working on a GUI in PyQt5 and I'm creating a window based off of a yaml settings file that contains information about what widgets should be contained within the window and some of their attributes. I have implemented it using just a single class but am thinking of refactoring it to use the factory design pattern but the issue is that certain widgets are composed of other widgets (tabs, stacked widgets) and I make a recursive call when creating the tabs and stacked widgets.
So this is a minimal example of what I have:
class WindowCreator():
def createWidget(self, parent, settings):
if settings.type == 'tab':
self.createTab(self, parent, settings)
if esttings.type == 'aTypeOfWidget':
self.createATypeOfWidget(self, parent, settings)
if settings.type == 'anotherTypeOfWidget':
self.createAnotherTypeOfWidget(self, parent, settings)
def createTab(self, parent, settings):
# create tab elements
for item in settings.items:
self.createWidget(self, parent, settings)
What I thought of doing was creating a simple abstract class that would define a create method and then create subclasses of that for each of the components.
class Base():
def create(parent, settings): ...
class Tab(Base):
def create(parent, settings):
#implementation that can call create for other derived classes
CheckBox.create(parent, settings)
#this is where I'm stuck
class CheckBox(Base):
def create(parent, settings):
#implementation that will only return a QCheckBox
Is there a way to do this within the factory design pattern? Or is there a better pattern that will keep the code cleaner?