访问者模式(Visitor)
定义
表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
动机
访问者模式适用于数据结构相对稳定的系统,它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由地演化。目地是要把处理从数据结构分离出来。
访问者模式(Visitor)结构图
![](https://i-blog.csdnimg.cn/blog_migrate/02d380279bc9c294e29ed611590a5ed0.jpeg)
Visitor类,为该对象结构中ConcreteElement的每一个类声明一个Visit操作。
abstract
class
Visitor
{
public abstract void VisitConcreteElementA(ConcreteElementA concreteElementA);
public abstract void VisitConcreteElementB(ConcreteElementA concreteElementB);
}
{
public abstract void VisitConcreteElementA(ConcreteElementA concreteElementA);
public abstract void VisitConcreteElementB(ConcreteElementA concreteElementB);
}
ConcreteVisitor1和ConcreteVisitor1类,具体访问者,实现每个由Visitor声明的操作。每个操作实现算法的一部分,而该算法片段乃是对应于结构中对象的类。
class
ConcreteVisitor1 : Visitor
{
public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
{
Console.WriteLine( " {0}被{1}访问 " ,concreteElementA.GetType().Name, this .GetType().Name);
}
public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
{
Console.WriteLine( " {0}被{1}访问 " , concreteElementB.GetType().Name, this .GetType().Name);
}
}
class ConcreteVisitor2 : Visitor
{
public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
{
Console.WriteLine( " {0}被{1}访问 " , concreteElementA.GetType().Name, this .GetType().Name);
}
public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
{
Console.WriteLine( " {0}被{1}访问 " , concreteElementB.GetType().Name, this .GetType().Name);
}
}
{
public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
{
Console.WriteLine( " {0}被{1}访问 " ,concreteElementA.GetType().Name, this .GetType().Name);
}
public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
{
Console.WriteLine( " {0}被{1}访问 " , concreteElementB.GetType().Name, this .GetType().Name);
}
}
class ConcreteVisitor2 : Visitor
{
public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
{
Console.WriteLine( " {0}被{1}访问 " , concreteElementA.GetType().Name, this .GetType().Name);
}
public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
{
Console.WriteLine( " {0}被{1}访问 " , concreteElementB.GetType().Name, this .GetType().Name);
}
}
Element类,定义一个Accept操作,它以一个访问者为参数。
abstract
class
Element
{
public abstract void Accept(Visitor visitor);
}
{
public abstract void Accept(Visitor visitor);
}
class
ConcreteElementA : Element
{
public override void Accept(Visitor visitor)
{
visitor.VisitConcreteElementA( this );
}
public void Operation()
{
}
}
class ConcreteElementB : Element
{
public override void Accept(Visitor visitor)
{
visitor.VisitConcreteElementB( this );
}
public void Operation()
{
}
}
{
public override void Accept(Visitor visitor)
{
visitor.VisitConcreteElementA( this );
}
public void Operation()
{
}
}
class ConcreteElementB : Element
{
public override void Accept(Visitor visitor)
{
visitor.VisitConcreteElementB( this );
}
public void Operation()
{
}
}
ObjectStructure类,能枚举它的元素,可以提供一个高层的接口以允许访问者访问它的元素
class
Objectstructure
{
private IList < Element > elements = new List < Element > ();
public void Attach(Element element)
{
elements.Add(element);
}
public void Detach(Element element)
{
elements.Remove(element);
}
public void Accept(Visitor visitor)
{
foreach (Element e in elements)
{
e.Accept(visitor);
}
}
}
{
private IList < Element > elements = new List < Element > ();
public void Attach(Element element)
{
elements.Add(element);
}
public void Detach(Element element)
{
elements.Remove(element);
}
public void Accept(Visitor visitor)
{
foreach (Element e in elements)
{
e.Accept(visitor);
}
}
}
static
void
Main(
string
[] args)
{
ObjectStructure o = new ObjectStructure();
o.Attach( new ConcreteElementA());
o.Attach( new ConcreteElementB());
ConcreteVisitor1 v1 = new ConcreteVisitor1();
ConcreteVisitor2 v2 = new ConcreteVisitor2();
o.Accept(v1);
o.Accept(v2);
}
{
ObjectStructure o = new ObjectStructure();
o.Attach( new ConcreteElementA());
o.Attach( new ConcreteElementB());
ConcreteVisitor1 v1 = new ConcreteVisitor1();
ConcreteVisitor2 v2 = new ConcreteVisitor2();
o.Accept(v1);
o.Accept(v2);
}