Notes and exercises for learning design patterns
Practice building a basic Facade that coordinates several subsystem components behind one clean entry point.
You are building a reporting tool for a small analytics company.
The tool needs to generate and deliver a weekly sales report. The process involves three steps:
The subsystem classes already exist and work correctly. They are defined
in exercise1.py. Your job is to build the ReportFacade that hides
their coordination from callers.
Implement the ReportFacade class so that this usage works:
facade = ReportFacade(
data_fetcher=SalesDataFetcher(),
formatter=ReportFormatter(),
emailer=ReportEmailer(),
)
facade.send_weekly_report(recipients=["alice@example.com", "bob@example.com"])
The Facade should:
data_fetcher.fetch_weekly_sales() to get the data.formatter.format(data) to produce a report string.emailer.send(report, recipients).The caller should not need to know about any of the subsystem classes or the order in which they are called.
ReportFacade.__init__ should accept the three subsystem objects as
constructor arguments (dependency injection).send_weekly_report.exercise1.py
Run the tests with:
python -m pytest exercise1.py -v