You are responsible for developing a program for generating tables. The table format is defined as follows:
<Table>
<Name> table name </name>
<Head>
<Col> name </col>
<Col> name </col>
......
</Head>
<Body>
<Line>
<N> content </n>
......
</Line>
......
</Body>
</Table>
Requirements:
1. The 2. in 3. Some <col> names and <n> contents in the table are allowed to be empty.
4. The table is generated from top down and from left to right row by row.
5. The final generated table must be balanced by rows and columns, and the blank part must be filled with null characters.
Reference answer
Design Points:
Use a coherent interface to design the table creation process
Because table Head involves a layer of nesting, and Body involves two layers of nesting, to facilitate adjustment and modification, each node element type must be retained back to the reference of the parent node.
1. Design Abstract node types with Fluent features
View sourceprint? 01 /// <summary>
02 // set type with Fluent features after modification
03 // </summary>
04 // <typeparam name = "T"> set element type </typeparam>
05 // <typeparam name = "TParent"> parent node type </typeparam>
06 class FluentCollection <TElement, TParent>
07 where TElement: class
08 where TParent: class
09 {
10 protected List <TElement> list = new List <TElement> ();
11 TParent parent;
12
13 public FluentCollection (TParent parent)
14 {
15 if (parent = null) throw new ArgumentNullException ("parent ");
16 this. parent = parent;
17}
18
19 /// <summary>
20 /// return the parent node
21 /// </summary>
22 <FONT style = "BACKGROUND-COLOR: # ffff00"> public TParent Parent {get {return parent ;}</FONT>
23
24 /// <summary>
25 // how to obtain the delegate of a TElement-type instance
26 /// </summary>
27 public Func <TElement> GetInstance {get; set ;}
28
29 /// <summary>
30 /// additional operations with fluent features
31 /// </summary>
32 // <param name = "t"> </param>
33 // <returns> </returns>
34 public FluentCollection <TElement, TParent> Add (TElement t)
35 {
36 list. Add (t );
37 <FONT style = "BACKGROUND-COLOR: # ffff00"> return this;
38 </FONT>}
39
40 /// <summary>
41 // vacant operation with fluent features
42 /// </summary>
43 // <returns> </returns>
44 public FluentCollection <TElement, TParent> Skip
45 {
46 get
47 {
48 list. Add (GetInstance ());
49 <FONT style = "BACKGROUND-COLOR: # ffff00"> return this; </FONT>
50}
51}
52
53 /// <summary>
54 // execute the foreach operation of LINQ
55 /// </summary>
56 /// <param name = "action"> </param>
57 public void ForEach (Action <TElement> action)
58 {
59 list. ForEach (action );
60}
61}
62
63 // <summary>
64 // elements whose parent node is table
65 /// </summary>
66 class WithTableObject
67 {
68 Table table; // parent node
69 public WithTableObject (Table table)
70 {
71 if (table = null) throw new ArgumentNullException ("table ");
72 this. table = table;
73}
74
75 /// <summary>
76 // point to the parent node -- table
77 // </summary>
78 <FONT style = "BACKGROUND-COLOR: # ffff00"> public Table Parent {get {return table ;}}
79 </FONT>}
2. Define each node type
View sourceprint? 001 class Notation
002 {
003 public Notation () {Data = string. Empty ;}
004 public Notation (string data) {Data = data ;}
005 public string Data {get; private set ;}
006}
007
008 // <summary>
009 // nelement
010 // </summary>
011 class Item: Notation
012 {
013 public Item (): base (){}
014 public Item (string data): base (data ){}
015}
016
017 /// <summary>
018 // col Element
019 /// </summary>
020 class Column: Notation
021 {
022 public Column (): base (){}
023 public Column (string data): base (data ){}
024}
025
026 /// <summary>
027 // line Element
028 /// </summary>
029 class Line
030 {