Now that we have had a look at the issues around object life cycle, let us return to our file system application and add life cycle operations to it, so clients can create and destroy files and directories.
To destroy a file or directory, the obvious choice is to add a
destroy operation to the
destroy can throw a
PermissionDenied exception. This is necessary because we must prevent attempts to destroy the root directory.
File interface is unchanged:
Note that, because
File derives from
Node, it inherits the
destroy operation we defined for
Directory interface now looks somewhat different from the previous version:
listoperation returns a sequence of structures instead of a list of proxies: for each entry in a directory, the
NodeDescstructure provides the name, type, and proxy of the corresponding file or directory.
- Directories provide a
findoperation that returns the description of the nominated node. If the nominated node does not exist, the operation throws a
createDirectoryoperations create a file and directory, respectively. If a file or directory already exists, the operations throw a
Here are the corresponding definitions:
Note that this design is somewhat different from the factory we designed for the phone book application. In particular, we do not have a single object factory; instead, we have as many factories as there are directories, that is, each directory creates files and directories only in that directory.
The motivation for this design is twofold:
- Because all files and directories that can be created are immediate descendants of their parent directory, we avoid the complexities of parsing path names for a separator such as "/". This keeps our example code to manageable size. (A real-world implementation of a distributed file system would, of course, be able to deal with path names.)
- Having more than one object factory presents interesting implementation issues that we will explore in the following discussion.
Let's move on to the implementation of this design in C++ and Java. You can find the full code of the implementation (including languages other than C++ and Java) in the
Manual/lifecycle directory of your Ice distribution.