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