名称 | Visitor |
结构 | |
意图 | 表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。 |
适用性 |
|
Code Example |
1// Visitor 2 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 7// For further information, read "Design Patterns", p331, Gamma et al., 8// Addison-Wesley, ISBN:0-201-63361-2 9 10/* 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 19namespace Visitor_DesignPattern 20{ 21 using System; 22 23 abstract class Visitor 24 { 25 abstract public void VisitElementA(ConcreteElementA a); 26 abstract public void VisitElementB(ConcreteElementB b); 27 } 28 29 class ConcreteVisitor1 : Visitor 30 { 31 override public void VisitElementA(ConcreteElementA a) 32 { 33 34 } 35 36 override public void VisitElementB(ConcreteElementB b) 37 { 38 39 } 40 } 41 42 abstract class Element 43 { 44 abstract public void Accept(Visitor v); 45 } 46 47 class ConcreteElementA : Element 48 { 49 public Visitor myVisitor; 50 override public void Accept(Visitor v) 51 { 52 myVisitor = v; 53 } 54 55 public void OperationA() 56 { 57 58 } 59 60 public void DoSomeWork() 61 { 62 // do some work here 63 // . . . 64 65 // Get visitor to visit 66 myVisitor.VisitElementA(this); 67 68 // do some more work here 69 // . . . 70 } 71 } 72 73 class ConcreteElementB : Element 74 { 75 override public void Accept(Visitor v) 76 { 77 78 } 79 80 public void OperationB() 81 { 82 83 } 84 } 85 86 /// <summary> 87 /// Summary description for Client. 88 /// </summary> 89 public class Client 90 { 91 public static int Main(string[] args) 92 { 93 ConcreteElementA eA = new ConcreteElementA(); 94 ConcreteElementB eB = new ConcreteElementB(); 95 ConcreteVisitor1 v1 = new ConcreteVisitor1(); 96 97 eA.Accept(v1); 98 eA.DoSomeWork(); 99 100 return 0; 101 } 102 } 103} 104 105 |