Python exercise · about 5 minutes · free
Abstract Factory: Families That Match
A dark-theme button next to a light-theme checkbox looks broken. Build whole families of objects from one factory.
A factory creates one kind of object. An Abstract Factory creates a family of related objects that must go together: UI widgets of one theme, database connection plus cursor plus dialect, cloud storage plus queue of one provider. The calling code asks a factory for each piece and never names the concrete classes, so swapping the family is a one-line change.
Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.
Your quest: Write
LightFactory and DarkFactory, each with create_button() and create_checkbox() returning the provided widget classes for that theme, and get_factory(name) returning an instance for "light" or "dark" (raise ValueError otherwise).Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
Each factory is a class with two methods returning the matching widget classes. <code>get_factory</code> looks the name up in a dict of classes.Output appears here.