日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]模版方法中的方法可以分為兩大類:模版方法(Template Method)和基本方法(Primitive Method)?! .模版方法  一個(gè)模版方法是定義在抽象類中的,把基本操作方法組合在一起形

模版方法中的方法可以分為兩大類:模版方法(Template Method)和基本方法(Primitive Method)。

  a.模版方法

  一個(gè)模版方法是定義在抽象類中的,把基本操作方法組合在一起形成一個(gè)總算法或一個(gè)總行為的方法。
這個(gè)模版方法一般會(huì)在抽象類中定義,并由子類不加以修改地完全繼承下來(lái)。

  b.基本方法

  基本方法又可以分為三種:抽象方法(Abstract Method)、具體方法(Concrete Method)和
鉤子方法(Hook Method)。

  抽象方法:一個(gè)抽象方法由抽象類聲明,由具體子類實(shí)現(xiàn)。在C#語(yǔ)言里一個(gè)抽象方法以abstract
關(guān)鍵字標(biāo)示出來(lái)。

  具體方法:一個(gè)具體方法由抽象類聲明并實(shí)現(xiàn),而子類并不實(shí)現(xiàn)或置換。在C#語(yǔ)言里面,一個(gè)具體方法
沒(méi)有abstract關(guān)鍵字。

  鉤子方法:一個(gè)鉤子方法由抽象類聲明并實(shí)現(xiàn),而子類會(huì)加以擴(kuò)展。通常抽象類給出的實(shí)現(xiàn)是一個(gè)空實(shí)現(xiàn),
作為方法的默認(rèn)實(shí)現(xiàn)。(Visual FoxPro中項(xiàng)目向?qū)Ы⒌捻?xiàng)目會(huì)使用一個(gè)AppHook類實(shí)現(xiàn)監(jiān)視項(xiàng)目成員變化,調(diào)整系統(tǒng)結(jié)構(gòu)的工作。)鉤子方法的名字通常以do開(kāi)始。



下面講解窗體基類的模板方法實(shí)現(xiàn):

抽象方法必須定義在抽象類中(Abstract Class),在某些場(chǎng)合不一定使用抽象類,比如frmBase是個(gè)具體類,因此我將模板方法改為虛擬(Virtual)的。

??????? ///?

????????///?在基類定義查詢數(shù)據(jù)的模板方法,由派生窗體實(shí)現(xiàn)具體的查詢功能。

????????///?

????????///?

????????protected?virtual?DataTable?DoSearch()

????????{

????????????return?new?DataTable();?//模板方法不能返回實(shí)際數(shù)據(jù)。

????????}



派生類具體實(shí)現(xiàn):


??????? //復(fù)寫(xiě)(Override)模板方法,具體實(shí)現(xiàn)

????????protected?override?DataTable?DoSearch()

????????{

????????????return?new?BLL_Product().Search(txtProductCode.Text, txtProductName.Text);

????????}



窗體基類界面:


public?partial?class?frmBase : Form
{
???public?frmBase()
???{
??????InitializeComponent();
???}
???
???private?void?btnSearch_Click(object?sender, EventArgs e)
???{
??????//調(diào)用模板方法?
??????DataTable data =?this.DoSearch();
??????
??????if?(data.Rows.Count > 0)
??????this.dataGridView1.DataSource = data;
??????else
??????MessageBox.Show("沒(méi)有找到數(shù)據(jù)!");
???}
???
???///??
???///??
???protected?virtual?DataTable DoSearch()
???{
??????return?new?DataTable();?//模板方法不能返回實(shí)際數(shù)據(jù)。?
???}
???
}


派生一個(gè)產(chǎn)品定義窗體:



public?partial?class?frmProduct : CSFramework.TemplateMethodDemo.frmBase
{
???public?frmProduct()
???{
??????InitializeComponent();
???}
???
???//復(fù)寫(xiě)(Override)模塊方法,具體實(shí)現(xiàn)?
???protected?override?DataTable DoSearch()
???{
??????return?new?BLL_Product().Search(txtProductCode.Text, txtProductName.Text);
???}
}


搜索功能在窗體基類實(shí)現(xiàn)了,我們只需要定義具體的數(shù)據(jù)查找方法。
通過(guò)重寫(xiě)DoSearch模塊方法,從業(yè)務(wù)邏輯層返回?cái)?shù)據(jù)。


///?

????///?業(yè)務(wù)邏輯層

????///?

????public?class?BLL_Product

????{

????????public?DataTable?Search(string?productCode,?string?productName)

????????{

????????????string?where =?" 1=1 ";

?

????????????if?(!String.IsNullOrEmpty(productCode))

????????????????where = where +?" and ProductCode=’"?+ productCode +?"’";

?

????????????if?(!String.IsNullOrEmpty(productName))?//支持模糊查找

????????????????where = where +?" and ProductName like ’%"?+ productName +?"%’";

?

????????????string?sql =?"select * from tb_Product where "?+ where;

????????????//從SQL Server?獲取數(shù)據(jù)?..........省略...........

????????????//

????????????//下面返回測(cè)試數(shù)據(jù)

????????????DataTable?test =?this.GetDemoData();//返回測(cè)試數(shù)據(jù)

????????????DataRow[] rows = test.Select(where);

?

????????????//克隆?System.Data.DataTable?的結(jié)構(gòu),包括所有?System.Data.DataTable?架構(gòu)和約束。

????????????DataTable?retTable = test.Clone();

????????????foreach?(DataRow?row?in?rows)

????????????????retTable.ImportRow(row);

?

????????????return?retTable;

????????}

?

????????///?

????????///?構(gòu)建測(cè)試數(shù)據(jù)表

????????///?

????????///?

????????private?DataTable?GetDemoData()

????????{

????????????DataTable?dt =?new?DataTable();

????????????dt.Columns.Add("ProductCode",?typeof(String));

????????????dt.Columns.Add("ProductName",?typeof(String));

????????????dt.Columns.Add("Price",?typeof(Decimal));

?

????????????dt.Rows.Add("001",?"原創(chuàng)作品CD", 99);

????????????dt.Rows.Add("002",?"Dev Express 9.2x Suit", 9960);

????????????dt.Rows.Add("003",?"可口可樂(lè)", 1.90);

????????????dt.Rows.Add("004",?"Engima CD", 269);

?

????????????return?dt;

????????}

????}



本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉