We have lost the guarantee we had with approach 1 that no one call call Print without the pre-requisite tasks getting called. Now we have to remember to always call our DoImportantStuffBeforePrinting method before anyone calls the LabelPrinter.Print method. This approach keeps our LabelPrinter class free from picking up any extra responsibilities, but it comes at a cost. remember to call this first before calling Print LabelDetails details = GetLabelDetails() Having seen that the LabelPrinter class was not really the best place for our new code to be added, the fallback approach is typically to put the new code in the calling class before it calls into the original method: private void DoPrint() Worse still, it tends to become untestable, as bits of GUI code or hard dependencies on the file system etc creep in. MessageBox.Show("Please attach the printer to COM1") Īnd before we know it, we have a class where the original functionality is swamped with miscellaneous concerns. If we allow ourselves to keep coding this way, before long, the Print method will become a magnet for special case features: public void Print(LabelDetails labelDetails) Our LabelPrinter class now has an extra responsibility. We have broken the Single Responsibility Principle. public void Print(LabelDetails labelDetails)ĭespite the fact that it works, this approach has several problems. This seems sensible – we run the new code at the last possible moment before performing the original task. Most developers would immediately gravitate towards going to the Print method, and putting their new code in there. Suppose a new requirement has come in that before it prints a label for a customer in Sweden, it needs to do some kind of postal code transformation. Consider a simple example of a LabelPrinter class, with a Print method. Before we do operation X, we want to do operation Y.
Where to put new windows sound schemes software#
Often we want to modify existing software by inserting an additional step.