设计模式之访问者(visitor)模式
   访问者(Visitor)模式作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作.Visitor模式实际上是分离了collection结构中的元素和对这些元素进行操作的行为.

 IDictionaryEnumerator iterator = collection. GetEnumerator ()
IDictionaryEnumerator iterator = collection. GetEnumerator () while (iterator. MoveNext ()) {
while (iterator. MoveNext ()) { Object o = iterator. Current;
   Object o = iterator. Current; if (o is Hashtable)
   if (o is Hashtable) messyPrintCollection((Hashtable)o);
      messyPrintCollection((Hashtable)o); else if (o is String)
   else if (o is String) Console.WriteLine("'"+o.ToString()+"'");
      Console.WriteLine("'"+o.ToString()+"'"); else if (o is Float)
   else if (o is Float) Console.WriteLine (o.ToString()+"f");
     Console.WriteLine (o.ToString()+"f"); else
   else Console.WriteLine (o.ToString());
     Console.WriteLine (o.ToString());
很显然,这样做的缺点代码If else if 很繁琐.我们就可以使用Visitor模式解决它.

   针对上例,定义接口叫Visitable,用来定义一个accept操作,也就是说让Collection每个元素具备可访问性.
被访问者是我们Collection的每个元素Element,我们要为这些Element定义一个可以接受访问的接口(访问和被访问是互动的,只访问者,被访问者如果表示不欢迎,访问者就不能访问),取名为Visitable.
被访问的具体元素继承这个新的接口Visitable:
 using System;
using System; using System.Collections;
using System.Collections;
 namespace ConsoleApplication4
namespace ConsoleApplication4 {
{ public interface Visitable
    public interface Visitable {
    { void accept(Visitor visitor);
        void accept(Visitor visitor); }
    }
 public class StringElement : Visitable
    public class StringElement : Visitable {
    { private String text = "";
        private String text = "";
 public StringElement(String txt)
        public StringElement(String txt) {
        { this.text = txt;
            this.text = txt; }
        }
 public String Text
        public String Text {
        { get
            get {
            { return this.text;
                return this.text; }
            } }
        } Visitable
        Visitable
 }
    }
 public class FloatElement : Visitable
    public class FloatElement : Visitable {
    { private float number = 0.0f;
        private float number = 0.0f;
 public FloatElement(float tmp)
        public FloatElement(float tmp) {
        { this.number = tmp;
            this.number = tmp; }
        }
 public float Number
        public float Number {
        { get
            get {
            { return this.number;
                return this.number; }
            } }
        } Visitable
        Visitable }
    }

 public interface Visitor
    public interface Visitor {
    { void visitString(StringElement stringE);
         void visitString(StringElement stringE); void visitFloat(FloatElement floatE);
         void visitFloat(FloatElement floatE); void visitCollection(ArrayList collection);
         void visitCollection(ArrayList collection);  }
    } /// <summary>
    /// <summary> /// ConcreteVisitor
    /// ConcreteVisitor /// </summary>
    /// </summary> public class ConcreteVisitor : Visitor
    public class ConcreteVisitor : Visitor {
    { public ConcreteVisitor()
        public ConcreteVisitor() {
        { //
            // // TODO:            //
            // TODO:            // }
        } Visitor
        Visitor }
    } }
}

 using System;
using System; using System.Collections;
using System.Collections;

 namespace ConsoleApplication4
namespace ConsoleApplication4 {
{ /// <summary>
    /// <summary> /// Class1
    /// Class1 /// </summary>
    /// </summary> class Class1
    class Class1 {
    { /// <summary>
        /// <summary> ///
        /// /// </summary>
        /// </summary> [STAThread]
        [STAThread] static void Main(string[] args)
        static void Main(string[] args) {
        { //
            // // TODO:            //
            // TODO:            //
 Visitor visitor = new ConcreteVisitor();
            Visitor visitor = new ConcreteVisitor();
 StringElement stringE = new StringElement("Hello,World");
            StringElement stringE = new StringElement("Hello,World"); visitor.visitString(stringE);
            visitor.visitString(stringE);
 FloatElement floatE = new FloatElement(4.2F);
            FloatElement floatE = new FloatElement(4.2F); visitor.visitFloat(floatE);
            visitor.visitFloat(floatE); 
             System.Collections.ArrayList list = new ArrayList();
            System.Collections.ArrayList list = new ArrayList(); list.Add(new StringElement("string1"));
            list.Add(new StringElement("string1")); list.Add(new StringElement("string2"));
            list.Add(new StringElement("string2")); list.Add(new FloatElement(3.4f));
            list.Add(new FloatElement(3.4f)); list.Add(new StringElement("string3"));
            list.Add(new StringElement("string3")); visitor.visitCollection(list);
            visitor.visitCollection(list);
 Console.Read();
            Console.Read();

 }
        } }
    } }
}

   我们设计一个接口visitor访问者,在这个接口中,有一些访问操作,这些访问操作是专门访问对象集合Collection中有可能的所有类,目前我们假定有三个行为:访问对象集合中的字符串类型;访问对象集合中的Float类型;
 StringElement只是一个实现,可以拓展为更多的实现,整个核心奥妙在accept方法中,在遍历Collection时,通过相应的accept方法调用具体类型的被访问者。这一步确定了被访问者类型,
如果是StringElement,而StringElement则回调访问者的visiteString方法,这一步实现了行为操作方法。
客户端代码中的list对象集合中放置了多种数据类型,对对象集合中的访问不必象一开始那样,使用is逐个判断,而是通过访问者模式巧妙实现了。
   使用访问者模式是对象群结构中(Collection) 中的对象类型很少改变。在两个接口Visitor和Visitable中,确保Visitable很少变化,也就是说,确保不能老有新的Element元素类型加进来,可以变化的是访问者行为或操作,也就是Visitor的不同子类可以有多种,这样使用访问者模式最方便.
   如果对象集合中的对象集合经常有变化, 那么不但Visitor实现要变化,Visistable也要增加相应行为,GOF建议是,不如在这些对象类中直接逐个定义操作,无需使用访问者设计模式.
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
posted on 2006-10-08 23:48 Phinecos(洞庭散人) 阅读(822) 评论(0) 收藏 举报
 
                    
                     
                    
                 
                    
                

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号 
