Finally, let callers choose a theme by name at runtime -- for example from a config file or a user setting. Build a small registry.
Implement get_factory(name) that:
Combined with build_ui from Step 4, a single string now drives the creation of an entire matching product family.
class Button:
"""Abstract product: a clickable button."""
def render(self):
raise NotImplementedError
class Checkbox:
"""Abstract product: a toggle checkbox."""
def render(self):
raise NotImplementedError
class LightButton(Button):
def render(self):
return "[Light Button]"
class LightCheckbox(Checkbox):
def render(self):
return "[Light Checkbox]"
class DarkButton(Button):
def render(self):
return "[Dark Button]"
class DarkCheckbox(Checkbox):
def render(self):
return "[Dark Checkbox]"
class LightFactory:
def create_button(self):
return LightButton()
def create_checkbox(self):
return LightCheckbox()
class DarkFactory:
def create_button(self):
return DarkButton()
def create_checkbox(self):
return DarkCheckbox()
def build_ui(factory):
button = factory.create_button()
checkbox = factory.create_checkbox()
return (button.render(), checkbox.render())
def get_factory(name):
# TODO: map "light" -> LightFactory() and "dark" -> DarkFactory().
# Raise ValueError for any unknown name.
passThe editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Finally, let callers choose a theme by name at runtime -- for example from a config file or a user setting. Build a small registry.
Implement get_factory(name) that:
Combined with build_ui from Step 4, a single string now drives the creation of an entire matching product family.
Press Run Tests or ββ© to check your solution.