访问者(Visitor)模式作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作.Visitor模式实际上是分离了collection结构中的元素和对这些元素进行操作的行为.
![](/Images/OutliningIndicators/None.gif)
IDictionaryEnumerator iterator = collection. GetEnumerator ()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
while (iterator. MoveNext ())
{
Object o = iterator. Current;
if (o is Hashtable)
messyPrintCollection((Hashtable)o);
else if (o is String)
Console.WriteLine("'"+o.ToString()+"'");
else if (o is Float)
Console.WriteLine (o.ToString()+"f");
else
Console.WriteLine (o.ToString());
很显然,这样做的缺点代码If else if 很繁琐.我们就可以使用Visitor模式解决它.
![2006100801.jpg](/images/cnblogs_com/phinecos/06925/2006100801.jpg)
针对上例,定义接口叫Visitable,用来定义一个accept操作,也就是说让Collection每个元素具备可访问性.
被访问者是我们Collection的每个元素Element,我们要为这些Element定义一个可以接受访问的接口(访问和被访问是互动的,只访问者,被访问者如果表示不欢迎,访问者就不能访问),取名为Visitable.
被访问的具体元素继承这个新的接口Visitable:
using System;
using System.Collections;
![](/Images/OutliningIndicators/None.gif)
namespace ConsoleApplication4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public interface Visitable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
void accept(Visitor visitor);
}
![](/Images/OutliningIndicators/InBlock.gif)
public class StringElement : Visitable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private String text = "";
![](/Images/OutliningIndicators/InBlock.gif)
public StringElement(String txt)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.text = txt;
}
![](/Images/OutliningIndicators/InBlock.gif)
public String Text
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.text;
}
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Visitable#region Visitable
![](/Images/OutliningIndicators/InBlock.gif)
public void accept(Visitor visitor)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO:
visitor.visitString(this);
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
public class FloatElement : Visitable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private float number = 0.0f;
![](/Images/OutliningIndicators/InBlock.gif)
public FloatElement(float tmp)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.number = tmp;
}
![](/Images/OutliningIndicators/InBlock.gif)
public float Number
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.number;
}
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Visitable#region Visitable
public void accept(Visitor visitor)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO:
visitor.visitFloat(this);
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
public interface Visitor
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
void visitString(StringElement stringE);
void visitFloat(FloatElement floatE);
void visitCollection(ArrayList collection);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// ConcreteVisitor
/// </summary>
public class ConcreteVisitor : Visitor
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ConcreteVisitor()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: //
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Visitor#region Visitor
public void visitString(StringElement stringE)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO:
Console.WriteLine(stringE.Text);
}
public void visitFloat(FloatElement floatE)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(floatE.Number);
}
![](/Images/OutliningIndicators/InBlock.gif)
public void visitCollection(ArrayList collection)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Collections.IEnumerator iterator = collection.GetEnumerator();
while(iterator.MoveNext())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
object o = iterator.Current;
if(o is Visitable)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
((Visitable)o).accept(this);
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
using System;
using System.Collections;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace ConsoleApplication4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Class1
/// </summary>
class Class1
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
[STAThread]
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: //
![](/Images/OutliningIndicators/InBlock.gif)
Visitor visitor = new ConcreteVisitor();
![](/Images/OutliningIndicators/InBlock.gif)
StringElement stringE = new StringElement("Hello,World");
visitor.visitString(stringE);
![](/Images/OutliningIndicators/InBlock.gif)
FloatElement floatE = new FloatElement(4.2F);
visitor.visitFloat(floatE);
System.Collections.ArrayList list = new ArrayList();
list.Add(new StringElement("string1"));
list.Add(new StringElement("string2"));
list.Add(new FloatElement(3.4f));
list.Add(new StringElement("string3"));
visitor.visitCollection(list);
![](/Images/OutliningIndicators/InBlock.gif)
Console.Read();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
我们设计一个接口visitor访问者,在这个接口中,有一些访问操作,这些访问操作是专门访问对象集合Collection中有可能的所有类,目前我们假定有三个行为:访问对象集合中的字符串类型;访问对象集合中的Float类型;
StringElement只是一个实现,可以拓展为更多的实现,整个核心奥妙在accept方法中,在遍历Collection时,通过相应的accept方法调用具体类型的被访问者。这一步确定了被访问者类型,
如果是StringElement,而StringElement则回调访问者的visiteString方法,这一步实现了行为操作方法。
客户端代码中的list对象集合中放置了多种数据类型,对对象集合中的访问不必象一开始那样,使用is逐个判断,而是通过访问者模式巧妙实现了。
使用访问者模式是对象群结构中(Collection) 中的对象类型很少改变。在两个接口Visitor和Visitable中,确保Visitable很少变化,也就是说,确保不能老有新的Element元素类型加进来,可以变化的是访问者行为或操作,也就是Visitor的不同子类可以有多种,这样使用访问者模式最方便.
如果对象集合中的对象集合经常有变化, 那么不但Visitor实现要变化,Visistable也要增加相应行为,GOF建议是,不如在这些对象类中直接逐个定义操作,无需使用访问者设计模式.