In this example, we are concerned with collecting the filenames of digital photos into named sets. We will not use actual images in this example, just filenames as strings. The client is given a domain-specific set of commands with which to create and manipulate the library. Central to the manipulation of the library, from the user's point of view, is "where we are." We start out at an empty set called "Album." Some of the commands leave the system at the component that has just been adjusted, whereas others move it back to the first component in the set. The commands are:
Add a new empty set with a name and stay there.
Add a new named photo after the pointer and stay there.
Find the named component (set or photo), or return null if it is not found.
Remove the named component (set or photo) and stay at the set from which it was removed.
Display the whole structure.
Exit the program.
Thus, the two operations that work on either
Remove. Consider some examples of the workings
of this system, illustrated in Example 3-2. The input commands are
shown in the middle, the result of
Display is shown on the left, and some
commentary appears on the right.
The input file contains all the commands in the middle. The program will expect this file to be called Composite.dat.
Example 3-2. Composite pattern—Photo Library sample run
AddSet Home AddPhoto Dinner.jpg AddSet Pets Going down another level AddPhoto ...