Element
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public interface Element 8 { 9 void Accept(Visitor visitor);10 }11}12
ConcereteElementA
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public class ConcereteElementA : Element 8 { 9 public ConcereteElementA()10 {11 }1213 Element 成员#region Element 成员1415 public void Accept(Visitor visitor)16 {17 visitor.VisitConcreteElementA(this);18 }1920 #endregion2122 public string OperationA()23 {24 return "This is in ConcereteElementA.OperationA()";25 }26 }27}28
ConcereteElementB
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public class ConcereteElementB : Element 8 { 9 Element 成员#region Element 成员1011 public void Accept(Visitor visitor)12 {13 visitor.VisitConcreteElementB(this);14 }1516 #endregion1718 public string OperationB()19 {20 return "This is in ConcereteElementB.OperationB()";21 }22 }23}
Visitor
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public interface Visitor 8 { 9 void VisitConcreteElementA(ConcereteElementA concereteElementA);10 void VisitConcreteElementB(ConcereteElementB concereteElementB);11 }12}
ConcereteVisitor1
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public class ConcereteVisitor1 : Visitor 8 { 9 Visitor 成员#region Visitor 成员1011 public void VisitConcreteElementA(ConcereteElementA concereteElementA)12 {13 Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);14 Console.WriteLine("we can visite ConcereteElementA's OperationA method"+ concereteElementA.OperationA());15 }1617 public void VisitConcreteElementB(ConcereteElementB concereteElementB)18 {19 Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);20 Console.WriteLine("we can visite ConcereteElementB's OperationB method");21 Console.WriteLine( concereteElementB.OperationB());22 }2324 #endregion25 }26}27
ConcereteVisitor2
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public class ConcereteVisitor2 : Visitor 8 { 9 Visitor 成员#region Visitor 成员1011 public void VisitConcreteElementA(ConcereteElementA concereteElementA)12 {13 Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);14 Console.WriteLine("we can visite ConcereteElementA's OperationA method:"+ concereteElementA.OperationA());15 }1617 public void VisitConcreteElementB(ConcereteElementB concereteElementB)18 {19 Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);20 Console.WriteLine("we can visite ConcereteElementB's OperationB method");21 Console.WriteLine( concereteElementB.OperationB());22 }2324 #endregion25 }26}27
ObjectStructure
1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Gof.Test.Visitor 6{ 7 public class ObjectStructure 8 { 9 private System.Collections.ArrayList items = new System.Collections.ArrayList();1011 public void Attach(Element element)12 {13 items.Add(element);14 }1516 public void Detach(Element element)17 {18 items.Remove(element);19 }2021 public void Accept(Visitor visitor)22 {23 foreach (Element e in items)24 {25 e.Accept(visitor);26 }27 }28 }29}30
客户代码
1 ObjectStructure os = new ObjectStructure(); 2 os.Attach(new ConcereteElementA()); 3 os.Attach(new ConcereteElementB()); 4 5 //create visitor objects 6 ConcereteVisitor1 v1 = new ConcereteVisitor1(); 7 ConcereteVisitor2 v2 = new ConcereteVisitor2(); 8 9 //accepting visitors10 os.Accept(v1);11 os.Accept(v2);1213 Console.ReadKey();
转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/01/17/622625.html
转载请注明原文地址: https://win8.8miu.com/read-1482758.html