12.8 Exercises
Add the os and pathlib calls to implement the methods for the File and Folder classes in the The Composite pattern section. The copy() method on File will need to read and write the bytes of a file. The copy() method on Folder is quite a bit more complicated, as you first have to duplicate the folder, and then recursively copy each of its children to the new location. The examples we provided update the internal data structure, but don’t apply changes to the operating system. Be careful about testing this in isolated directories. You don’t want to accidentally destroy important files.
The Composite example has an alternative design, also. Consider how this could be built using a Facade class to contain operations from pathlib and shutil. Which implementation of Folder-File move, copy, and remove operations seems easier to understand? Which is easier to test?
As in the previous chapter, look at the patterns we’ve discussed and...