设计模式——访问者模式

名称Visitor
结构 Visitor.gif
意图表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
适用性
  • 一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依赖于其具体类的操作。
  • 需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而你想避免让这些操作“污染”这些对象的类。Vi s i t o r 使得你可以将相关的操作集中起来定义在一个类中。当该对象结构被很多应用共享时,用Vi s i t o r 模式让每个应用仅包含需要用到的操作。
  • 定义对象结构的类很少改变,但经常需要在此结构上定义新的操作。改变对象结构类需要重定义对所有访问者的接口,这可能需要很大的代价。如果对象结构类经常改变,那么可能还是在这些类中定义这些操作较好。
Code Example
  1 None.gif //  Visitor
  2 None.gif
  3 None.gif //  Intent: "Represent an operation to be performed on the elements of an 
  4 None.gif //  object structure. Visitor lets you define a new operation without 
  5 None.gif //  changing the classes of the elements on which it operates." 
  6 None.gif
  7 None.gif //  For further information, read "Design Patterns", p331, Gamma et al.,
  8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
  9 None.gif
 10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
 11InBlock.gif * If you have a number of elements, and wish to carry out a number of
 12InBlock.gif * operations on them, the Visitor design pattern can be helpful.
 13InBlock.gif * 
 14InBlock.gif * It lets you extract the operations to be carried out on elements from
 15InBlock.gif * the elements themselves. It means operations cna change without affecting
 16InBlock.gif * the elements. 
 17ExpandedBlockEnd.gif */

 18 None.gif 
 19 None.gif namespace  Visitor_DesignPattern
 20 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 21InBlock.gif    using System;
 22InBlock.gif
 23InBlock.gif    abstract class Visitor 
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 25InBlock.gif        abstract public void VisitElementA(ConcreteElementA a);
 26InBlock.gif        abstract public void VisitElementB(ConcreteElementB b);
 27ExpandedSubBlockEnd.gif    }

 28InBlock.gif
 29InBlock.gif    class ConcreteVisitor1 : Visitor
 30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 31InBlock.gif        override public void VisitElementA(ConcreteElementA a)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            
 34ExpandedSubBlockEnd.gif        }

 35InBlock.gif
 36InBlock.gif        override public void VisitElementB(ConcreteElementB b)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38InBlock.gif            
 39ExpandedSubBlockEnd.gif        }

 40ExpandedSubBlockEnd.gif    }

 41InBlock.gif
 42InBlock.gif    abstract class Element 
 43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 44InBlock.gif        abstract public void Accept(Visitor v);
 45ExpandedSubBlockEnd.gif    }

 46InBlock.gif
 47InBlock.gif    class ConcreteElementA : Element 
 48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 49InBlock.gif        public Visitor myVisitor;
 50InBlock.gif        override public void Accept(Visitor v)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            myVisitor = v;            
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif
 55InBlock.gif        public void OperationA()
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 57InBlock.gif            
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif
 60InBlock.gif        public void DoSomeWork()
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            // do some work here
 63InBlock.gif            // . . .
 64InBlock.gif
 65InBlock.gif            // Get visitor to visit
 66InBlock.gif            myVisitor.VisitElementA(this);        
 67InBlock.gif
 68InBlock.gif            // do some more work here
 69InBlock.gif            // . . .
 70ExpandedSubBlockEnd.gif        }

 71ExpandedSubBlockEnd.gif    }

 72InBlock.gif
 73InBlock.gif    class ConcreteElementB : Element 
 74ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 75InBlock.gif        override public void Accept(Visitor v)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            
 78ExpandedSubBlockEnd.gif        }

 79InBlock.gif
 80InBlock.gif        public void OperationB()
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 82InBlock.gif            
 83ExpandedSubBlockEnd.gif        }

 84ExpandedSubBlockEnd.gif    }

 85InBlock.gif
 86ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 87InBlock.gif    ///    Summary description for Client.
 88ExpandedSubBlockEnd.gif    /// </summary>

 89InBlock.gif    public class Client
 90ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 91InBlock.gif        public static int Main(string[] args)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
 93InBlock.gif            ConcreteElementA eA = new ConcreteElementA();
 94InBlock.gif            ConcreteElementB eB = new ConcreteElementB();
 95InBlock.gif            ConcreteVisitor1 v1 = new ConcreteVisitor1();
 96InBlock.gif
 97InBlock.gif            eA.Accept(v1);
 98InBlock.gif            eA.DoSomeWork();
 99InBlock.gif
100InBlock.gif            return 0;
101ExpandedSubBlockEnd.gif        }

102ExpandedSubBlockEnd.gif    }

103ExpandedBlockEnd.gif}

104 None.gif
105 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/19/218153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值