访问者模式:表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变个元素的类的前提下定义作用于这些元素的新操作。
适用于数据结构相对稳定的系统,如下示例的男人和女人。男人和女人是固定的
示例的UML图如下:
状态抽象类(Action)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { //状态类 abstract class Action { public abstract void GetManReflect(Man man); //获取男人的反映 public abstract void GetWomanReflect(Woman woman); //获取女人的反映 } }
成功状态(Success)继承于状态抽象类(Action)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { //成功状态 class Success : Action { public override void GetManReflect(Man man) { Console.WriteLine("{0},{1}时,背后多半有个伟大的女人。",man.GetType().Name,this.GetType().Name); } public override void GetWomanReflect(Woman woman) { Console.WriteLine("{0},{1}时,背后大多有一个不成功的男人。", woman.GetType().Name, this.GetType().Name); } } }
失败状态(Failing)继承于状态抽象类(Action)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { class Failing :Action { public override void GetManReflect(Man man) { Console.WriteLine("{0},{1}时,闷头喝酒,谁也不用劝。", man.GetType().Name, this.GetType().Name); } public override void GetWomanReflect(Woman woman) { Console.WriteLine("{0},{1}时,眼泪汪汪,谁也劝不了", woman.GetType().Name, this.GetType().Name); } } }
人类(Person)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { abstract class Person { protected string action; public string Action { get { return action; } set { action = value; } } /// <summary> /// 得出结论 /// </summary> public abstract void GetConclusion(Action action); } }
男人类(Man)继承于人类(Person)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { class Man:Person { public override void GetConclusion(Action action) { action.GetManReflect(this); } } }
女人类(Woman)继承于人类(Person)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { class Woman:Person { public override void GetConclusion(Action action) { action.GetWomanReflect(this); } } }
对象结构类(ObjectStruction) 双分派技术:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VisitorPattern.CLASS { //对象结构 class ObjectStructure { private IList<Person> elements = new List<Person>(); //增加 public void Attach(Person element) { elements.Add(element); } //移除 public void Detach(Person element) { elements.Remove(element); } //查看显示 public void Display(Action action) { foreach (Person person in elements) { person.GetConclusion(action); } } } }
测试类(TestMain)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using VisitorPattern.CLASS; namespace VisitorPattern { class TeatMain { static void Main(string[] args) { ObjectStructure obj = new ObjectStructure(); obj.Attach(new Man()); obj.Attach(new Woman()); Success su = new Success(); obj.Display(su); Failing fa = new Failing(); obj.Display(fa); Console.Read(); } } }
测试结果:
要么忍,要么狠,要么滚!