二.Template Method Pattern(模版方法模式)

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Template Method Pattern.aspx.cs "  Inherits = " Pattern_Template_Method_Pattern "   %>
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > Untitled Page </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:TextBox  ID ="tb_result"  runat ="server"  TextMode ="MultiLine"  Rows ="5" ></ asp:TextBox >
        
< asp:Button  ID ="bt_submit"  runat ="server"  OnClick ="bt_submit_Click"   Text ="Template Method Pattern"   />  
    
</ div >
    
</ form >
</ body >
</ html >


using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

///   <summary>
///  模版方法模式
///  優點:可以先定義好共用的流程,在具體實現各個細節.可擴展性好!
///   </summary>
public  partial  class  Pattern_Template_Method_Pattern : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {

    }
    
protected   void  bt_submit_Click( object  sender, EventArgs e)
    {        
        Concrete c 
=   new  Concrete();
        tb_result.Text 
=  c.TemplateMethod();
    }
}
///   <summary>
///  抽象的模板類
///   </summary>
public   abstract   class  AbstractTemplate
{
    
public   abstract   string  Operation1();
    
public   abstract   string  Operation2();

    
public   string  TemplateMethod() // 鉤子方法,也可以在'具體實現摸板類'擴充
    {
        
return  Operation1()  +   "  and  "   +  Operation2();
    }
}
///   <summary>
///  具體實現摸板類
///   </summary>
public   class  Concrete : AbstractTemplate
{
    
public   override   string  Operation1()
    {
        
return   " Operation1 " ;
    }
    
public   override   string  Operation2()
    {
        
return   " Operation2 " ;
    }
}

你可能感兴趣的:(二.Template Method Pattern(模版方法模式))