大話設計模式 - 第37章 (2/2)

Console.WriteLine(Message:+this.Content);

foreach(IThreadtinChildren)

{

t.RenderContent();

}

}

}

}

工廠類為:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Data;

namespaceCompositeStudy

{

《summary》

///工廠類

///《/summary》

///《remarks》工廠類《/remarks》

publicclassThreadFactory

{

DataTabletable=newDataTable();

publicThreadFactory()

{

table.Columns.Add(“content”);

table.Columns.Add(“IsTop”);

table.Columns.Add(“IsMessage”);

table.Columns.Add(“ID”);

table.Columns.Add(“ParentID”);

DataRowrow=table.NewRow();

row[“content”]=“test”;

row[“IsTop”]=false;

row[“IsMessage”]=false;

row[“ID”]=1;

row[“ParentID”]=0;

table.Rows.Add(row);

row=table.NewRow();

row[“content”]=“test1”;

row[“IsTop”]=true;

row[“IsMessage”]=false;

row[“ID”]=0;

row[“ParentID”]=-1;

table.Rows.Add(row);

row=table.NewRow();

row[“content”]=“test2”;

row[“IsTop”]=false;

row[“IsMessage”]=true;

row[“ID”]=2;

row[“ParentID”]=0;

table.Rows.Add(row);

row=table.NewRow();

row[“content”]=“test3”;

row[“IsTop”]=false;

row[“IsMessage”]=true;

row[“ID”]=3;

row[“ParentID”]=0;

table.Rows.Add(row);

}

publicList《IThread》GetTopThreads()

{

List《IThread》list=newList《IThread》();

DataRow[]rows=table.Select(“IsTop=true”);

foreach(DataRowrowinrows)

{

Threadt=newThread();

t.Content=row[“content”].ToString();

t.IsTop=true;

DataRow[]cs=table.Select(“ParentID=”+Convert.ToInt32(row[“ID”]));

foreach(DataRowrincs)

{

{

Messagem=newMessage();

m.Content=r[“content”].ToString();

m.IsTop=false;

t.Add(m);

}

else

{

Threadtt=newThread();

tt.IsTop=false;

t.Add(tt);

}

}

list.Add(t);

}

returnlist;

}

}

}

客戶端調뇾方法為:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceCompositeStudy

{

classProgram

{

staticvoidMain(string[]args)

{

ThreadFactoryfactory=newThreadFactory();

List《IThread》threads=factory.GetTopThreads();

foreach(IThreadtinthreads)

{

t.RenderContent();

}

Console.Read();

}

}

}

上一章|目錄|下一章