Here is the payoff. Write a client function build_ui(factory) that assembles a screen using any factory. It should ask the factory for a button and a checkbox, render both, and return their strings as a tuple (button_string, checkbox_string).
Because the client only talks to the factory interface, the same code produces a fully consistent Light UI or a fully consistent Dark UI -- you never get a light button next to a dark checkbox.
Implement build_ui(factory).
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):
# TODO: ask the factory for a button and a checkbox,
# then return their rendered strings as a tuple (button, checkbox).
passThe editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Here is the payoff. Write a client function build_ui(factory) that assembles a screen using any factory. It should ask the factory for a button and a checkbox, render both, and return their strings as a tuple (button_string, checkbox_string).
Because the client only talks to the factory interface, the same code produces a fully consistent Light UI or a fully consistent Dark UI -- you never get a light button next to a dark checkbox.
Implement build_ui(factory).
Press Run Tests or ββ© to check your solution.