Design Patterns

Notes and exercises for learning design patterns

View the Project on GitHub Claptar/design-patterns

Exercise 4: Monostate

Goal

Implement Monostate: many objects, one shared state.

Singleton says:

one object

Monostate says:

many objects, one shared state

In this exercise, Preferences() should create different objects, but all Preferences objects should share the same attributes.

What you will practice

Starting point

Open exercise4.py.

Fill the TODOs so that:

  1. Preferences() creates different objects.
  2. Different Preferences objects share the same __dict__.
  3. Changing one object’s attribute is visible through another object.
  4. Later Preferences(...) calls do not reset the first initialization.
  5. Different subclasses do not accidentally share state.

Expected behavior

first = Preferences(theme="light")
second = Preferences(theme="dark")

assert first is not second
assert first.__dict__ is second.__dict__
assert second.theme == "light"

first.theme = "dark"
assert second.theme == "dark"

Run the tests

python -m pytest exercise4.py