Using a factory method
One way to fix this problem would be to add a simple test to the ProcessData method and write out three zeros (or some other value). This solution is simple but provides no educational value, so I decided to do something different. My solution is to simply not create an output file if the input data is empty.
Once we do that, the code doesn't always need the IWriter interface. It is therefore wasteful to create this interface before we call the ProcessData method. We can instead use a lazy initialization pattern, which I already mentioned while discussing different singleton implementations.
The lazy initialization will be covered in the next section, but it is so simple and intuitive that I won't confuse you if I use ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access