Design Pattern Examples
Overview of object-oriented design patterns
Composite_FileDirEntry.cs
Go to the documentation of this file.
1
7
8using System;
9using System.Collections.Generic;
10
12{
17 public enum FileDirTypes
18 {
22 File,
23
28 }
29
30
36 public class FileDirEntry
37 {
43 {
44 get;
45 protected set;
46 }
47
51 public virtual string Name
52 {
53 get;
54 protected set;
55 }
56
61 public virtual long Length
62 {
63 get;
64 protected set;
65 }
66
70 public virtual DateTime WhenModified
71 {
72 get;
73 protected set;
74 }
75
80 public virtual FileDirEntry[]? Children
81 {
82 get
83 {
84 return null;
85 }
86 }
87
91 public FileDirEntry()
92 {
94 Name = string.Empty;
95 Length = 0;
96 WhenModified = DateTime.MinValue;
97 }
98
99 }
100
101
102 //########################################################################
103 //########################################################################
104
105
112 {
113 public FileEntry(string name, long size, DateTime modDate)
114 {
116 Name = name;
117 Length = size;
118 WhenModified = modDate;
119 }
120 }
121
122
123 //########################################################################
124 //########################################################################
125
126
136 {
137 List<FileDirEntry> _children;
140
148 public DirEntry(string name, DateTime modDate, FileDirEntry[] children)
149 {
150 if (children == null)
151 {
152 throw new ArgumentNullException("children", "The list of children cannot be null but can be empty.");
153 }
154 FileDirType = FileDirTypes.Directory;
155 Name = name;
156 WhenModified = modDate;
157 _children = new List<FileDirEntry>(children);
158 }
159
160
167 public override long Length
168 {
169 get
170 {
171 if (!_lengthSet)
172 {
173 // Recurse over all children, accumulating their size.
174 foreach(FileDirEntry entry in Children)
175 {
176 _length += entry.Length;
177 }
178 _lengthSet = true;
179 }
180 return _length;
181 }
182 }
183
184
192 public override FileDirEntry[] Children
193 {
194 get
195 {
196 return _children.ToArray();
197 }
198 }
199 }
200}
Represents a Directory entry.
bool _lengthSet
long _length
DirEntry(string name, DateTime modDate, FileDirEntry[] children)
Construct a DirEntry instance.
override long Length
Retrieve the size of all children of this directory. The length is calculated on the first call and c...
override FileDirEntry[] Children
Retrieve the children of this node.
List< FileDirEntry > _children
Base class representing a File or Directory entry.
virtual long Length
The length in bytes of this entry. Directory entries are the sum of the length of all children.
virtual ? FileDirEntry[] Children
The children of this entry. Is null if the entry can never have any children (that is,...
virtual DateTime WhenModified
When this entry was last modified.
FileDirEntry()
Default constructor.
virtual string Name
The name of this entry.
virtual FileDirTypes FileDirType
The type of this entry as represented by a value from the FileDirTypes enumeration.
Represents a File entry.
FileEntry(string name, long size, DateTime modDate)
The namespace containing all Design Pattern Examples implemented in C#.
FileDirTypes
Represents the type of entries allowed in the hierarchy for the Composite design pattern example.
@ Directory
Represents a directory entry that can contain other FileDirEntry components.