Note that there are some explanatory texts on larger screens.

plurals
  1. POComposite pattern for file and folder structure with parent reference in C#
    primarykey
    data
    text
    <p>I'm currently struggling with the implementation of a set of file system classes. I guess this calls for the composite pattern if I'm not mistaken. So I set up the following classes:</p> <p>An abstract class <code>Node</code> which has a reference to its parent folder and two classes <code>Folder</code> and <code>File</code> that implement <code>Node</code>. A folder contains a collection of all its children and methods to add and remove children.</p> <p>The thing is, I can't figure out how to implement all the methods properly. In all the examples I have seen there is no reference to the parent in the children. How can the <code>AddChild</code> method ensure that the child's parent reference is set correctly? I solved that by checking whether <code>child.Parent</code> has already been set to the folder or it throws an <code>ArgumentException</code>. The matter is further complicated by the fact that <code>AddChild</code> might also throw an exception like <code>DuplicateNameException</code> or something. So my methods look like this now:</p> <pre><code>File.AddTo(Folder folder) { this.Parent = folder; try { folder.AddChild(this); } catch { this.Parent = null; throw; } } Folder.AddChild(Node child) { if(child.Parent != this) throw new ArgumentException(...); ... } </code></pre> <p>Now I have this ugly <code>AddTo</code> method and cannot do something like <code>someFolder.AddChild(new File(...))</code>. I wonder how it was implemented with <code>ListViewItem</code> for instance. There I can just do <code>someListView.Items.Add(new ListViewItem(...))</code>.</p> <p>My solution works, but I'm not convinced that it's the right way to do this. Maybe someone has a better solution or can point me to a good example. Thanks in advance.</p> <p><strong>EDIT</strong>: Minimal full class definitions below.</p> <pre><code>abstract class Node { public Folder Parent { get; protected set; } public string Name { get; private set; } public Node(string name) { Parent = null; Name = name; } } class Folder : Node { private Dictionary&lt;string, Node&gt; _children; public Folder(string name) : base(name) { // Other initializations here... } public void AddChild(Node child) { if(child is Folder) ((Folder)child).Parent = this; // Damn, doesn't work for files!!! else if(child.Parent != this) throw new ArgumentException(); if(_children.ContainsKey(child.Name)) throw new DuplicateNameException(); _children[child.Name] = child; } } class File : Node { public File(string name) : base(name) { // Other initializations here... } public void AddTo(Folder folder) { Parent = folder; try { folder.AddChild(this); } catch { Parent = null; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload