Code Example |
1 // Visitor 2![](/Images/OutliningIndicators/None.gif) 3 // Intent: "Represent an operation to be performed on the elements of an 4 // object structure. Visitor lets you define a new operation without 5 // changing the classes of the elements on which it operates." 6![](/Images/OutliningIndicators/None.gif) 7 // For further information, read "Design Patterns", p331, Gamma et al., 8 // Addison-Wesley, ISBN:0-201-63361-2 9![](/Images/OutliningIndicators/None.gif) 10![](/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Notes: 11 * If you have a number of elements, and wish to carry out a number of 12 * operations on them, the Visitor design pattern can be helpful. 13 * 14 * It lets you extract the operations to be carried out on elements from 15 * the elements themselves. It means operations cna change without affecting 16 * the elements. 17 */ 18 19 namespace Visitor_DesignPattern 20![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) { 21 using System; 22![](/Images/OutliningIndicators/InBlock.gif) 23 abstract class Visitor 24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 25 abstract public void VisitElementA(ConcreteElementA a); 26 abstract public void VisitElementB(ConcreteElementB b); 27 } 28![](/Images/OutliningIndicators/InBlock.gif) 29 class ConcreteVisitor1 : Visitor 30![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 31 override public void VisitElementA(ConcreteElementA a) 32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 33 34 } 35![](/Images/OutliningIndicators/InBlock.gif) 36 override public void VisitElementB(ConcreteElementB b) 37![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 38 39 } 40 } 41![](/Images/OutliningIndicators/InBlock.gif) 42 abstract class Element 43![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 44 abstract public void Accept(Visitor v); 45 } 46![](/Images/OutliningIndicators/InBlock.gif) 47 class ConcreteElementA : Element 48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 49 public Visitor myVisitor; 50 override public void Accept(Visitor v) 51![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 52 myVisitor = v; 53 } 54![](/Images/OutliningIndicators/InBlock.gif) 55 public void OperationA() 56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 57 58 } 59![](/Images/OutliningIndicators/InBlock.gif) 60 public void DoSomeWork() 61![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 62 // do some work here 63 // . . . 64![](/Images/OutliningIndicators/InBlock.gif) 65 // Get visitor to visit 66 myVisitor.VisitElementA(this); 67![](/Images/OutliningIndicators/InBlock.gif) 68 // do some more work here 69 // . . . 70 } 71 } 72![](/Images/OutliningIndicators/InBlock.gif) 73 class ConcreteElementB : Element 74![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 75 override public void Accept(Visitor v) 76![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 77 78 } 79![](/Images/OutliningIndicators/InBlock.gif) 80 public void OperationB() 81![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 82 83 } 84 } 85![](/Images/OutliningIndicators/InBlock.gif) 86![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary> 87 /// Summary description for Client. 88 /// </summary> 89 public class Client 90![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 91 public static int Main(string[] args) 92![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 93 ConcreteElementA eA = new ConcreteElementA(); 94 ConcreteElementB eB = new ConcreteElementB(); 95 ConcreteVisitor1 v1 = new ConcreteVisitor1(); 96![](/Images/OutliningIndicators/InBlock.gif) 97 eA.Accept(v1); 98 eA.DoSomeWork(); 99![](/Images/OutliningIndicators/InBlock.gif) 100 return 0; 101 } 102 } 103 } 104![](/Images/OutliningIndicators/None.gif) 105![](/Images/OutliningIndicators/None.gif) |