Notes and exercises for learning design patterns
Practice a more realistic Prototype implementation.
In real code, deepcopy() is not always the whole answer.
Sometimes a clone should:
The dashboard now has a DataSource and a cache.
DataSource -> shared configuration for where data comes from
cache -> temporary computed results
When cloning a dashboard:
widgets should be independent,filters should be independent,data_source should be shared,cache should start empty.Why?
The data source is shared configuration. The clone should point to the same source.
The cache is temporary runtime data. The clone should not inherit old cached results.
Open exercise3.py and implement:
def clone(self, **changes) -> "Dashboard":
...
The method should create a new Dashboard manually or carefully copy the fields.
The clone must satisfy these rules:
Dashboard object,widgets,filters,data_source,cache,AttributeError.Run:
python exercise3.py
The tests check all clone rules above.
Do not blindly use deepcopy(self) as the whole implementation.
A good implementation can use deepcopy() only for selected fields:
widgets=deepcopy(self.widgets)
filters=deepcopy(self.filters)
Then intentionally reuse:
data_source=self.data_source
And intentionally reset:
cache={}