Notes and exercises for learning design patterns
Practice combining Prototype with Factory.
The factory should choose a preconfigured dashboard prototype, clone it, customize it, and return the new dashboard.
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:
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):
...
The factory should:
Run:
python exercise4.py
The tests check that:
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