Topics / 🏗️ Design Patterns (GoF) / 🏗️ Level 84: Design Patterns: Creational

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.

Diagram. Client code; Dark factory (one family); Dark button; Dark checkbox. Links: Client code → Dark factory; Dark factory → Dark button; Dark factory → Dark checkboxClient codeDark factoryone familyDark buttonDark checkbox
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.
HintEach 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.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 84: Design Patterns: Creational

Share on LinkedIn