Design Patterns

Notes and exercises for learning design patterns

View the Project on GitHub Claptar/design-patterns

Exercise 4: Dashboard Prototype Factory

Goal

Practice combining Prototype with Factory.

The factory should choose a preconfigured dashboard prototype, clone it, customize it, and return the new dashboard.


Story

Your company now has two standard dashboard templates:

sales dashboard
marketing dashboard

Each one has different widgets and default filters.

Callers should not manually clone those templates. They should use a clear factory method:

dashboard = DashboardFactory.new_sales_dashboard(
    title="UK Sales Dashboard",
    owner="Alice",
    region="UK",
)

The caller should not know the street-level details of the prototype:


Your task

Open exercise4.py and implement DashboardFactory.

The Dashboard.clone() method is already implemented from the previous exercise.

Implement:

@classmethod
def _new_dashboard(cls, prototype, title, owner, region):
    ...

Then implement:

@classmethod
def new_sales_dashboard(cls, title, owner, region):
    ...

@classmethod
def new_marketing_dashboard(cls, title, owner, region):
    ...

Required behavior

The factory should:

  1. pick the right prototype,
  2. clone it,
  3. set the title,
  4. set the owner,
  5. set the region filter,
  6. return the customized dashboard.

Tests to satisfy

Run:

python exercise4.py

The tests check that:


Mental model

DashboardFactory
    sales_dashboard prototype
    marketing_dashboard prototype

new_sales_dashboard(...)
    clone sales prototype
    customize title, owner, region

new_marketing_dashboard(...)
    clone marketing prototype
    customize title, owner, region

This is the same idea as a prototype factory:

choose prototype -> clone -> customize -> return