Template Method or Strategy? When is the choice made
transcript
Same exporter, two designs. Template Method inherits the steps. Strategy hands them in. Both print the same files. So which one?
In Template Method, the order lives in the base class. Subclasses supply the steps. In Strategy, the order lives in one concrete class. The steps are objects you pass in. So where the order lives doesn't decide it.
A subclass is fixed when the object is built. That exporter is a PDF exporter for life. Say a dropdown picks the format. Or one exporter must make two formats in a row. Then the format has to be an object you hand in.
Inherit when the steps need the base class's own helpers and fields. They can use its protected state directly, with nothing to wire up. And the format never changes after the object is built. In that case, inheriting is simpler, and there is one object fewer.
So ask when the variation is chosen. At runtime? Hand it in. Fixed for life, and the steps need the base class? Inherit.
Full breakdown in the lecture.