程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 《模式——工程化實現及擴展》連貫接口 Fluent Interface—“自我檢驗 及參考答案

《模式——工程化實現及擴展》連貫接口 Fluent Interface—“自我檢驗 及參考答案

編輯:C#入門知識

由你負責開發一個生成表格的程序,表格的格式定義如下:

 

<table>
    <name>表格名稱</name>
    <head>
        <col>名稱</col>
        <col>名稱</col>
        ......
    </head>
    <body>
        <line>
            <n>內容</n>
            ......
        </line>
        ......
    </body>
</table>

要求:
1、<head>和<body>部分都是可選項
2、<head>中<col>部分是可選項、<body>中<line>部分是可選項、<line>中<n>部分是可選項
3、允許表格中某些<col>的名稱和<n>的內容為空
4、表格自上向下、自左向右逐行生成
5、最終生成的表格必須是行列平衡的,空白部分用空字符填滿

參考答案

 

設計要點:

采用連貫接口設計表格的創建過程
由於表格Head涉及一層嵌套、Body涉及兩層嵌套,因此為了便於調整和修改,每個節點元素類型都要保留回溯到父節點的引用
 

1、設計具有Fluent特征的抽象節點類型

view sourceprint?01 /// <summary> 

02 /// 修改後具有Fluent特征的集合類型 

03 /// </summary> 

04 /// <typeparam name="T">集合元素類型</typeparam> 

05 /// <typeparam name="TParent">父節點類型</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     /// 返回父節點 

21     /// </summary> 

22    <FONT > public TParent Parent{get{ return parent;}}</FONT> 

23   

24     /// <summary> 

25     /// 如何獲得一個TElement類型實例的委托 

26     /// </summary> 

27     public Func<TElement> GetInstance { get; set; } 

28   

29     /// <summary> 

30     /// 具有fluent特征的追加操作 

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 >return this; 

38 </FONT>    } 

39   

40     /// <summary> 

41     /// 具有fluent特征的空置操作 

42     /// </summary> 

43     /// <returns></returns> 

44     public FluentCollection<TElement, TParent> Skip 

45     { 

46         get

47         { 

48             list.Add(GetInstance()); 

49             <FONT >return this;</FONT> 

50         } 

51     } 

52   

53     /// <summary> 

54     /// 執行LINQ的foreach操作 

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 /// 父節點為table的元素 

65 /// </summary> 

66 class WithTableObject 

67 { 

68     Table table;    //  父節點 

69     public WithTableObject(Table table) 

70     { 

71         if(table == null) throw new ArgumentNullException("table"); 

72         this.table = table; 

73     } 

74   

75     /// <summary> 

76     /// 指向父節點——table 

77     /// </summary> 

78     <FONT >public Table Parent{get{ return table;}} 

79 </FONT>}

 

2、定義各個節點類型

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 /// n元素 

010 /// </summary> 

011 class Item : Notation 

012 { 

013     public Item():base(){} 

014     public Item(string data) : base(data){} 

015 } 

016   

017 /// <summary> 

018 /// col 元素 

019 /// </summary> 

020 class Column : Notation 

021 { 

022     public Column():base(){} 

023     public Column(string data) : base(data) { } 

024 } 

025   

026 /// <summary> 

027 /// line 元素  

028 /// </summary> 

029 class Line 

030 { 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved