Exercises
Before diving into exercises for each design pattern, take a moment to implement the copy
method for the File
and Folder
objects in the previous section. The File
method should be quite trivial; just create a new node with the same name and contents, and add it to the new parent folder. 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 it's children to the new location. You can call the copy()
method on the children indiscriminately, regardless of whether each is a file or a folder object. This will drive home just how powerful the composite pattern can be.
Now, as with the previous chapter, look at the patterns we've discussed, and consider ideal places where you might implement them. You may want to apply the adapter pattern to existing code, as it is most often applicable when interfacing with existing libraries, rather than new code. How can you use an adapter to force two interfaces to interact...