封裝乙個樹形選單一 類設計

2021-08-26 10:42:43 字數 3372 閱讀 8986

[我有廢話要說]

pa的u。m介面的樹也是我封裝的,做這個功能的時候,我希望它可以像smart-gwt的treegrid一樣好用。

不過我不會c#,也第一次寫asp控制項,還想讓他像treegrid……在這個難度上,我想再加點,把我的樹寫的通用又好維護,於是我決定好好設計,加入設計模式的思想:)

[正文——封個控制項來畫樹]

需求:

領導說寫個讀取xml配置,讓後封裝成樹,在介面上展示出來。

我是個畫蛇添足的程式設計師,我想分兩步完成,希望這個題目能兩天就寫完。

第一步:

從xml解析一棵樹,放到tree中。並且考慮:如果xml格式變了、如果不從xml取資料庫……要讓我的**盡量滿足開閉原則。

第二步:

做乙個渲染樹的控制項,將這個樹丟到控制項中,樹就畫出來了,可以多列。

開始幹活:

我的樹的設計

使用者通過工廠類得到乙個xml工廠。

xmltreereader工廠解析xml檔案,把xml配置的指定解析自己的itreereader返回。(比如扁平或層級的表示樹形的xml檔案)

itreereader一拿到就可以開始幹活了,itreereader解析xml的節點,把資料封裝到樹結構itreenode中。

這裡需要注意的是:itreenode表示乙個樹的樹根節點,有節點的屬性,還有子節點。因此itreereader解析時候,需要建立子節點,也就是itreenode物件。而itreenode是介面,不能new出來,所以itreereader需要知道itreenode的實現類,於是我傳入itreereader乙個itreenode的實現類,反射來建立物件。

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace com.origin.base.test.mywork.tree

}

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace com.origin.base.test.mywork.tree

public xmltreerreade***ctory(string path)

public itreereader createtreereader()}}

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace com.origin.base.test.mywork.tree

}

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace com.origin.base.test.mywork.tree

public itreenode createtree()

private itreenode createnode()

//再次用到反射

type type = type.gettype(treenodeclassname);

itreenode treenode = (itreenode)type.assembly.createinstance(treenodeclassname);

return treenode;}}

}

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace com.origin.base.test.mywork.tree

//設定當前節點的key-value對

public abstract void putattribute(object key, object value);

//根據key得到對應的值

public abstract object getattribute(object key);

//為當前節增加乙個子節點,在它的節點的下一級節點的末尾增加

public abstract void addsubnode(itreenode node);

public abstract ilistgetsubnodes();}}

using system;

using system.collections.generic;

using system.linq;

using system.web;

using system.collections;

namespace com.origin.base.test.mywork.tree

//根據key得到對應的值

public override object getattribute(object key)

else

}//為當前節增加乙個子節點,在它的節點的下一級節點的末尾增加

public override void addsubnode(itreenode node)

subnodes.add(node);

}public override ilistgetsubnodes()}}

看看client呼叫:

using system;

using system.collections.generic;

using system.linq;

using system.text;

using com.origin.base.test.mywork.tree;

static void main(string args)

public static void printtree(itreenode node,string s)

for (int i = 0; i < subnodes.count; i++)}}

}

輸出:

記乙個遞迴封裝樹形結構

最近改了個小bug,原有的資料字典查詢,封裝成樹的遞迴寫的有問題,就自己寫了乙個,解決這種遞迴的思考是 分析樹形結構 找到父子層之間的關聯關係 根據關係編寫遞迴巢狀條件。很簡單也很實用,遂總結如下 擷取表結構如下,關聯id 和pid 父id 其中樹最外層pid為0 邏輯 public list ge...

設計乙個類

1 設計乙個不能被繼承的類 1 將建構函式設為私有 此時子類不能訪問基類的建構函式,因此建立子類時就會報錯 無法訪問private成員 class base uninherit base uninherit const base uninherit rhs base uninherit operat...

乙個封裝的C Socket類

乙個封裝的c socket類 2011年06月03日 include stdafx.h include libyfun.h include mytcp.h include ty server.h 建構函式 myctcp myctcp 析構函式 myctcp myctcp 賦值 入參 nsockfd ...