The abstract factories

Step 3 -- The abstract factories

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:

  • LightFactory.create_button() returns a LightButton, and create_checkbox() returns a LightCheckbox.
  • DarkFactory.create_button() returns a DarkButton, and create_checkbox() returns a DarkCheckbox.

Each factory must only ever produce products from its own theme.

Hints
Starter code
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
        pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.