1

I am currently issuing a problem while forming the program-design, which is exact like a File-Directory relationship, and to ease it, I am using that as an example.

I think it is usual, that a Directory has a collection of it's File instances.
I take this as kinda axiom, correct me if this is not usual.
But , when there are events occuring on a file, and the directory wants to be notified with those changes, the file has to have a reference to it's directory in some way:

  • A) The direct way:
    The File class has a reference to it's Directory instance.
  • B) Through Callbacks/Event-Handling:
    The File class has a reference to a function of it's Directory instance.

OR

  • C) The Directory checks the properties of the File in an intervall.
    I really hate this 'solution' because I think it is unperfomant as hell.

Both (A, B) ways are creating a circular dependency (many cons) and require the Directory of a File to be instantiated before itself, unless I lazy-init the Files reference (should I?).

So how are those File-Directory relationships designed? Do they have a circular dependency, an intervall check, do they use lazy-init...?

2

You have made a potentially invalid assumption in your design: it is not necessarily true that there is a unique directory which contains any given file. A file may exist but not be linked in any directory (if it has been deleted but still has open handles, at least in some implementations) or may exist in multiple directories (if it has been hard-linked).

It also seems likely that objects other than directories may care about updates to files, so why not simply use an Observer pattern here, without putting any special behaviour on "the" parent of the file?

1

But , when there are events occuring on a file, and the directory wants to be notified with those changes, the file has to have a reference to it's directory in some way ...

There's no way to do that, at least on any of the dominant operating systems, and even many of the minor operating systems. There are two problems. One is that files do not know the directory to which they belong. The other perhaps even bigger problem is that on many operating systems (e.g., Windows, Linux, OSX), a file cannot know which directory it belongs to because a file can belong to multiple directories.

Just as there is a one-to-many relationship between directories and files, there can be a one-to-many relationship between files and directories. A file can be a member of multiple directories, thanks to hard and soft links. Operating systems do not maintain the one-to-many relationship between files and directories.

Files are typically very simple things. On most operating systems, there is no metadata associated with a file that indicates the directory (or directories) to which a file belongs.


Does a a file have to own a reference to it's dir?

Not usually.

0

In the Directory-File relationship, there is no real need for a file to be aware to what Directory it belongs. If we know (1) the root directory and (2) for each Directory which other Directories and Files it contains, we have sufficient knowledge to build a directory tree.

This basically follows the Composite design pattern.

  • So there's a global (static? singleton?) Directory-Tree which is accessible by all directories/files? And each file/directory is able to locate itself there and therefore to access it's parents/childs without referencing them? – user3698624 Mar 4 '16 at 19:19
  • @user3698624 Not necessarily. Again, there is no real need for any file or directory to know it's parent, but it is possible to implement this. Just have a look at the Composite design pattern and it should (hopefully) become more clear. – Miguel van de Laar Mar 4 '16 at 19:23
0

In the real world you would not have such a model, since that would load the entire file system into memory just by accessing a single file or directory.

Rather you would have "lazy loading", so a file object only loads parents or children when requested. A Directory object would have a method (not a property!) which returns its children as a collection of File and Directory objects (only one level deep), and File and Directory objects would have method which returns the parent directory.

You could have a live collection of items which was updated based on changes in the underlying file system. But you wouldn't need to propagate changes upwards in the hierarchy.

  • [...] and File and Directory objects would have method which returns the parent directory. But this requires a File to have a reference to it's parent Directory doesnt it? – user3698624 Mar 4 '16 at 19:16
  • @user3698624: No, it would just need to know how to load the parent directory when requested. – JacquesB Mar 4 '16 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.