四.Visitor Pattern(訪問者模式)

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Visitor Pattern.aspx.cs "  Inherits = " Pattern_Visitor_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"  Width ="400px" ></ asp:TextBox >
        
< asp:Button  ID ="bt_submit"  runat ="server"  OnClick ="bt_submit_Click"   Text ="VisitorPattern"   />     
        
< select  id ="strategy_select"  runat ="server" >
            
< option  value ="A"  selected ="selected" > A </ option >
            
< option  value ="B" > B </ option >            
        
</ select >
    
</ 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_Visitor_Pattern : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {

    }

    
private   void  VisitorFactory()
    {
        Context ttt 
=   null ;
        
switch  (strategy_select.Items[strategy_select.SelectedIndex].Value.ToString())
        {

            
case   " A " :
                ConcreteVisitorA VA 
=   new  ConcreteVisitorA();
                tb_result.Text 
=  VA.VisitorAccessObjectA( new  AccessObjectA())  +   "  \n "   +  VA.VisitorAccessObjectB( new  AccessObjectB());
                
break ;
            
case   " B " :
                ConcreteVisitorB VB 
=   new  ConcreteVisitorB();
                tb_result.Text 
=  VB.VisitorAccessObjectA( new  AccessObjectA())  +   "  \n "   +  VB.VisitorAccessObjectB( new  AccessObjectB());
                
break ;           
            
default :
                tb_result.Text 
=   " 請選擇 " ;
                
break ;
        }

    }
    
protected   void  bt_submit_Click( object  sender, EventArgs e)
    {
        VisitorFactory();
    }
}

///   <summary>
///  先定義一個訪問者
///   </summary>
public   abstract   class  Visitor
{
    
public   abstract   string  VisitorAccessObjectA(AccessObjectA accessobjecta);
    
public   abstract   string  VisitorAccessObjectB(AccessObjectB accessobjectb);
}

///   <summary>
///  定義一個訪問對象的抽象類
///   </summary>
public   abstract   class  AccessObject
{
    
public   abstract   string  GetVisitorName();
}

public   class  AccessObjectA : AccessObject
{
    
public   override   string  GetVisitorName()
    {
        
return   " ObjectA " ;
    }
    
public   string  OperatorA()
    {
        
return   " OperatorA " ;
    }
}
public   class  AccessObjectB : AccessObject
{
    
public   override   string  GetVisitorName()
    {
        
return   " ObjectB " ;
    }
    
public   string  OperatorB()
    {
        
return   " OperatorB " ;
    }
}
///   <summary>
///  實現Visitor A類
///   </summary>
public   class  ConcreteVisitorA:Visitor
{   
    
public   override   string  VisitorAccessObjectA(AccessObjectA accessobjecta)
    {
        
return  accessobjecta.GetVisitorName()  + "  visited by  " + this ;
    }
    
public   override   string  VisitorAccessObjectB(AccessObjectB accessobjectb)
    {
        
return  accessobjectb.GetVisitorName()  +   "  visited by  "   +   this ;
    }
}
///   <summary>
///  實現Visitor B類
///   </summary>
public   class  ConcreteVisitorB : Visitor
{
    
public   override   string  VisitorAccessObjectA(AccessObjectA accessobjecta)
    {
        
return  accessobjecta.GetVisitorName()  +   "  visited by  "   +   this ;
    }
    
public   override   string  VisitorAccessObjectB(AccessObjectB accessobjectb)
    {
        
return  accessobjectb.GetVisitorName()  +   "  visited by  "   +   this ;
    }
}

你可能感兴趣的:(Pattern)