Design Pattern Examples
Overview of object-oriented design patterns
Composite_Exercise.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Text;
8
10{
22 internal class Composite_Exercise
23 {
24
35 {
36 const int NAME_PADDING_SIZE = 20;
37 StringBuilder output = new StringBuilder();
38 string spaces = new string(' ', depth * 2);
39 output.AppendFormat("{0}{1}", spaces, entry.Name);
40 int padding = NAME_PADDING_SIZE - entry.Name.Length - (depth * 2);
41 if (entry.FileDirType == FileDirTypes.Directory)
42 {
43 output.Append("/");
44 padding--;
45 }
46 output.AppendFormat("{0}", new string(' ', padding));
47 output.AppendFormat("{0,4}", entry.Length);
48 output.AppendFormat(" {0}", entry.WhenModified.ToString());
49 output.AppendFormat("{0}", Environment.NewLine);
50
51 FileDirEntry[]? children = entry.Children;
52 if (children != null)
53 {
54 for (int index = 0; index < children.Length; ++index)
55 {
56 output.AppendFormat(Composite_Exercise_FormatEntry(children[index], depth + 1));
57 }
58 }
59
60 return output.ToString();
61 }
62
63
70 {
71 string output = Composite_Exercise_FormatEntry(entry, 2);
72 Console.WriteLine(output);
73 }
74
75
79 // ! [Using Composite in C#]
80 public void Run()
81 {
82 Console.WriteLine();
83 Console.WriteLine("Composite Exercise");
84 try
85 {
86 string filepath = "root";
87 FileDirEntry rootEntry = Composite_FileAccess.GetEntry(filepath);
88 Console.WriteLine(" Showing object '{0}'", filepath);
90
91 filepath = "root/subdir1/FileD.txt";
92 rootEntry = Composite_FileAccess.GetEntry(filepath);
93 Console.WriteLine(" Showing object '{0}'", filepath);
95 }
96 catch (System.IO.FileNotFoundException e)
97 {
98 Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
99 }
100 Console.WriteLine(" Done.");
101 }
102 // ! [Using Composite in C#]
103 }
104}
Example of using the Composite Pattern in C#.
void Run()
Executes the example for the Composite Pattern in C#.
string Composite_Exercise_FormatEntry(FileDirEntry entry, int depth)
Format the specified entry for display.
void Composite_Exercise_ShowEntry(FileDirEntry entry)
Recursively display the contents of the hierarchical list of objects starting with the given object.
Static class containing functions for accessing a hardcoded "file" and "directory" hierarchy.
static FileDirEntry GetEntry(string filepath)
Return a FileDirEntry object representing the specified file "path" in an internal list of data entri...
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.
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.
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.