|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace VisitorPattern |
| 4 | +{ |
| 5 | +// Visitee |
| 6 | +interface IAnimal |
| 7 | +{ |
| 8 | + void Accept(IAnimalOperation operation); |
| 9 | +} |
| 10 | + |
| 11 | +// Visitor |
| 12 | +interface IAnimalOperation |
| 13 | +{ |
| 14 | + void VisitMonkey(Monkey monkey); |
| 15 | + void VisitLion(Lion lion); |
| 16 | + void VisitDolphin(Dolphin dolphin); |
| 17 | +} |
| 18 | + |
| 19 | +class Monkey : IAnimal |
| 20 | +{ |
| 21 | + public void Shout() |
| 22 | + { |
| 23 | + Console.WriteLine("Oooh o aa aa!"); |
| 24 | + } |
| 25 | + |
| 26 | + public void Accept(IAnimalOperation operation) |
| 27 | + { |
| 28 | + throw new NotImplementedException(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class Lion : IAnimal |
| 33 | +{ |
| 34 | + public void Roar() |
| 35 | + { |
| 36 | + Console.WriteLine("Roaar!"); |
| 37 | + } |
| 38 | + |
| 39 | + public void Accept(IAnimalOperation operation) |
| 40 | + { |
| 41 | + throw new NotImplementedException(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class Dolphin : IAnimal |
| 46 | +{ |
| 47 | + public void Speak() |
| 48 | + { |
| 49 | + Console.WriteLine("Tuut tittu tuutt!"); |
| 50 | + } |
| 51 | + |
| 52 | + public void Accept(IAnimalOperation operation) |
| 53 | + { |
| 54 | + throw new NotImplementedException(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class Speak : IAnimalOperation |
| 59 | +{ |
| 60 | + public void VisitDolphin(Dolphin dolphin) |
| 61 | + { |
| 62 | + dolphin.Speak(); |
| 63 | + } |
| 64 | + |
| 65 | + public void VisitLion(Lion lion) |
| 66 | + { |
| 67 | + lion.Roar(); |
| 68 | + } |
| 69 | + |
| 70 | + public void VisitMonkey(Monkey monkey) |
| 71 | + { |
| 72 | + monkey.Shout(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +class Jump : IAnimalOperation |
| 77 | +{ |
| 78 | + public void VisitDolphin(Dolphin dolphin) |
| 79 | + { |
| 80 | + Console.WriteLine("Jumped 20 feet high! on to the tree!"); |
| 81 | + } |
| 82 | + |
| 83 | + public void VisitLion(Lion lion) |
| 84 | + { |
| 85 | + Console.WriteLine("Jumped 7 feet! Back on the ground!"); |
| 86 | + } |
| 87 | + |
| 88 | + public void VisitMonkey(Monkey monkey) |
| 89 | + { |
| 90 | + Console.WriteLine("Walked on water a little and disappeared!"); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + class Program |
| 96 | + { |
| 97 | + static void Main(string[] args) |
| 98 | + { |
| 99 | +var monkey = new Monkey(); |
| 100 | +var lion = new Lion(); |
| 101 | +var dolphin = new Dolphin(); |
| 102 | + |
| 103 | +var speak = new Speak(); |
| 104 | + |
| 105 | +monkey.Accept(speak); // Ooh oo aa aa! |
| 106 | +lion.Accept(speak); // Roaaar! |
| 107 | +dolphin.Accept(speak); // Tuut tutt tuutt! |
| 108 | + |
| 109 | +var jump = new Jump(); |
| 110 | + |
| 111 | +monkey.Accept(speak); // Ooh oo aa aa! |
| 112 | +monkey.Accept(jump); // Jumped 20 feet high! on to the tree! |
| 113 | + |
| 114 | +lion.Accept(speak); // Roaaar! |
| 115 | +lion.Accept(jump); // Jumped 7 feet! Back on the ground! |
| 116 | + |
| 117 | +dolphin.Accept(speak); // Tuut tutt tuutt! |
| 118 | +dolphin.Accept(jump); // Walked on water a little and disappeared |
| 119 | + |
| 120 | + Console.ReadLine(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments