Visitor 表示一个作用于某个对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作

 Element
Element1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public interface Element
    public interface Element8

 
     {
{9
 void Accept(Visitor visitor);
        void Accept(Visitor visitor);10
 }
    }11
 }
}12


 ConcereteElementA
ConcereteElementA 1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public class ConcereteElementA : Element
    public class ConcereteElementA : Element8

 
     {
{9
 public ConcereteElementA()
        public ConcereteElementA()10

 
         {
{11
 }
        }12

13

 Element 成员#region Element 成员
        Element 成员#region Element 成员14

15
 public void Accept(Visitor visitor)
        public void Accept(Visitor visitor)16

 
         {
{17
 visitor.VisitConcreteElementA(this);
            visitor.VisitConcreteElementA(this);18
 }
        }19

20
 #endregion
        #endregion21

22
 public string OperationA()
        public string OperationA()23

 
         {
{24
 return "This is in ConcereteElementA.OperationA()";
            return "This is in ConcereteElementA.OperationA()";25
 }
        }26
 }
    }27
 }
}28


 ConcereteElementB
ConcereteElementB 1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public class ConcereteElementB : Element
    public class ConcereteElementB : Element8

 
     {
{9

 Element 成员#region Element 成员
        Element 成员#region Element 成员10

11
 public void Accept(Visitor visitor)
        public void Accept(Visitor visitor)12

 
         {
{13
 visitor.VisitConcreteElementB(this);
            visitor.VisitConcreteElementB(this);14
 }
        }15

16
 #endregion
        #endregion17

18
 public string OperationB()
        public string OperationB()19

 
         {
{20
 return "This is in ConcereteElementB.OperationB()";
            return "This is in ConcereteElementB.OperationB()";21
 }
        }22
 }
    }23
 }
}
 Visitor
Visitor1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public interface Visitor
    public interface Visitor8

 
     {
{9
 void VisitConcreteElementA(ConcereteElementA concereteElementA);
        void VisitConcreteElementA(ConcereteElementA concereteElementA);10
 void VisitConcreteElementB(ConcereteElementB concereteElementB);
        void VisitConcreteElementB(ConcereteElementB concereteElementB);11
 }
    }12
 }
}
 ConcereteVisitor1
ConcereteVisitor1 1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public class ConcereteVisitor1 : Visitor
    public class ConcereteVisitor1 : Visitor8

 
     {
{9

 Visitor 成员#region Visitor 成员
        Visitor 成员#region Visitor 成员10

11
 public void VisitConcreteElementA(ConcereteElementA concereteElementA)
        public void VisitConcreteElementA(ConcereteElementA concereteElementA)12

 
         {
{13
 Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);
            Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);14
 Console.WriteLine("we can visite ConcereteElementA's OperationA method"+ concereteElementA.OperationA());
            Console.WriteLine("we can visite ConcereteElementA's OperationA method"+ concereteElementA.OperationA());15
 }
        }16

17
 public void VisitConcreteElementB(ConcereteElementB concereteElementB)
        public void VisitConcreteElementB(ConcereteElementB concereteElementB)18

 
         {
{19
 Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);
            Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);20
 Console.WriteLine("we can visite ConcereteElementB's OperationB method");
            Console.WriteLine("we can visite ConcereteElementB's OperationB method");21
 Console.WriteLine( concereteElementB.OperationB());
            Console.WriteLine( concereteElementB.OperationB());22
 }
        }23

24
 #endregion
        #endregion25
 }
    }26
 }
}27


 ConcereteVisitor2
ConcereteVisitor2 1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public  class ConcereteVisitor2 : Visitor
   public  class ConcereteVisitor2 : Visitor8

 
     {
{9

 Visitor 成员#region Visitor 成员
        Visitor 成员#region Visitor 成员10

11
 public void VisitConcreteElementA(ConcereteElementA concereteElementA)
        public void VisitConcreteElementA(ConcereteElementA concereteElementA)12

 
         {
{13
 Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);
            Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);14
 Console.WriteLine("we can visite ConcereteElementA's OperationA method:"+ concereteElementA.OperationA());
            Console.WriteLine("we can visite ConcereteElementA's OperationA method:"+ concereteElementA.OperationA());15
 }
        }16

17
 public void VisitConcreteElementB(ConcereteElementB concereteElementB)
        public void VisitConcreteElementB(ConcereteElementB concereteElementB)18

 
         {
{19
 Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);
            Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);20
 Console.WriteLine("we can visite ConcereteElementB's OperationB method");
            Console.WriteLine("we can visite ConcereteElementB's OperationB method");21
 Console.WriteLine( concereteElementB.OperationB());
            Console.WriteLine( concereteElementB.OperationB());22
 }
        }23

24
 #endregion
        #endregion25
 }
    }26
 }
}27


 ObjectStructure
ObjectStructure1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace Gof.Test.Visitor
namespace Gof.Test.Visitor6


 {
{7
 public class ObjectStructure
    public class ObjectStructure8

 
     {
{9
 private System.Collections.ArrayList items = new System.Collections.ArrayList();
        private System.Collections.ArrayList items = new System.Collections.ArrayList();10

11
 public void Attach(Element element)
        public void Attach(Element element)12

 
         {
{13
 items.Add(element);
            items.Add(element);14
 }
        }15

16
 public void Detach(Element element)
        public void Detach(Element element)17

 
         {
{18
 items.Remove(element);
            items.Remove(element);19
 }
        }20

21
 public void Accept(Visitor visitor)
        public void Accept(Visitor visitor)22

 
         {
{23
 foreach (Element e in items)
            foreach (Element e in items)24

 
             {
{25
 e.Accept(visitor);
                e.Accept(visitor);26
 }
            }27
 }
        }28
 }
    }29
 }
}30


 客户代码
客户代码1
 ObjectStructure os = new ObjectStructure();
            ObjectStructure os = new ObjectStructure();2
 os.Attach(new ConcereteElementA());
            os.Attach(new ConcereteElementA());3
 os.Attach(new ConcereteElementB());
            os.Attach(new ConcereteElementB());4

5
 //create visitor objects
            //create visitor objects6
 ConcereteVisitor1 v1 = new ConcereteVisitor1();
            ConcereteVisitor1 v1 = new ConcereteVisitor1();7
 ConcereteVisitor2 v2 = new ConcereteVisitor2();
            ConcereteVisitor2 v2 = new ConcereteVisitor2();8

9
 //accepting visitors
            //accepting visitors10
 os.Accept(v1);
            os.Accept(v1);11
 os.Accept(v2);
            os.Accept(v2);12

13
 Console.ReadKey();
            Console.ReadKey();.jpg) 
  
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号