namespace VistorPattern { // 抽象元素角色 public abstract class Element { public abstract void Accept(IVistor vistor); public abstract void Print(); } // 具体元素A public class ElementA :Element { public override void Accept(IVistor vistor) { // 调用访问者visit方法 vistor.Visit(this); } public override void Print() { Console.WriteLine("我是元素A"); } } // 具体元素B public class ElementB :Element { public override void Accept(IVistor vistor) { vistor.Visit(this); } public override void Print() { Console.WriteLine("我是元素B"); } } // 抽象访问者 public interface IVistor { void Visit(ElementA a); void Visit(ElementB b); } // 具体访问者 public class ConcreteVistor :IVistor { // visit方法而是再去调用元素的Accept方法 public void Visit(ElementA a) { a.Print(); } public void Visit(ElementB b) { b.Print(); } } // 对象结构 public class ObjectStructure { private ArrayList elements = new ArrayList(); public ArrayList Elements { get { return elements; } } public ObjectStructure() { Random ran = new Random(); for (int i = 0; i < 6; i++) { int ranNum = ran.Next(10); if (ranNum > 5) { elements.Add(new ElementA()); } else { elements.Add(new ElementB()); } } } } class Program { static void Main(string[] args) { ObjectStructure objectStructure = new ObjectStructure(); foreach (Element e in objectStructure.Elements) { // 每个元素接受访问者访问 e.Accept(new ConcreteVistor()); } Console.Read(); } } }
本文介绍了一种设计模式——访客模式,并通过一个具体的.NET实现案例展示了该模式的应用。访客模式允许向一组已经存在的类中增加新的行为,而无需修改这些类的代码。文中提供了一个包含多种元素和访问者的实例,演示了如何使用访客模式遍历和操作这些元素。
1759

被折叠的 条评论
为什么被折叠?



