Now we hide the concrete classes behind factories. A factory is an object whose job is to produce one whole family of matching products. The client will later depend only on the factory interface, never on LightButton or DarkButton directly.
Implement two factories:
Each factory must only ever produce products from its own theme.
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):
# TODO: return a matching LightButton instance
pass
def create_checkbox(self):
# TODO: return a matching LightCheckbox instance
pass
class DarkFactory:
def create_button(self):
# TODO: return a matching DarkButton instance
pass
def create_checkbox(self):
# TODO: return a matching DarkCheckbox instance
passThe editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Now we hide the concrete classes behind factories. A factory is an object whose job is to produce one whole family of matching products. The client will later depend only on the factory interface, never on LightButton or DarkButton directly.
Implement two factories:
Each factory must only ever produce products from its own theme.
Press Run Tests or ββ© to check your solution.