|
2 | 2 |
|
3 | 3 | namespace VisitorPattern |
4 | 4 | { |
5 | | - // Visitee |
6 | | - interface IAnimal |
| 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() |
7 | 22 | { |
8 | | - void Accept(IAnimalOperation operation); |
| 23 | + Console.WriteLine("Oooh o aa aa!"); |
9 | 24 | } |
10 | 25 |
|
11 | | - // Visitor |
12 | | - interface IAnimalOperation |
| 26 | + public void Accept(IAnimalOperation operation) |
13 | 27 | { |
14 | | - void VisitMonkey(Monkey monkey); |
15 | | - void VisitLion(Lion lion); |
16 | | - void VisitDolphin(Dolphin dolphin); |
| 28 | + operation.VisitMonkey(this); |
17 | 29 | } |
| 30 | +} |
18 | 31 |
|
19 | | - class Monkey : IAnimal |
| 32 | +class Lion : IAnimal |
| 33 | +{ |
| 34 | + public void Roar() |
20 | 35 | { |
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 | | - } |
| 36 | + Console.WriteLine("Roaar!"); |
30 | 37 | } |
31 | 38 |
|
32 | | - class Lion : IAnimal |
| 39 | + public void Accept(IAnimalOperation operation) |
33 | 40 | { |
34 | | - public void Roar() |
35 | | - { |
36 | | - Console.WriteLine("Roaar!"); |
37 | | - } |
38 | | - |
39 | | - public void Accept(IAnimalOperation operation) |
40 | | - { |
41 | | - throw new NotImplementedException(); |
42 | | - } |
| 41 | + operation.VisitLion(this); |
43 | 42 | } |
| 43 | +} |
44 | 44 |
|
45 | | - class Dolphin : IAnimal |
| 45 | +class Dolphin : IAnimal |
| 46 | +{ |
| 47 | + public void Speak() |
46 | 48 | { |
47 | | - public void Speak() |
48 | | - { |
49 | | - Console.WriteLine("Tuut tittu tuutt!"); |
50 | | - } |
| 49 | + Console.WriteLine("Tuut tittu tuutt!"); |
| 50 | + } |
51 | 51 |
|
52 | | - public void Accept(IAnimalOperation operation) |
53 | | - { |
54 | | - throw new NotImplementedException(); |
55 | | - } |
| 52 | + public void Accept(IAnimalOperation operation) |
| 53 | + { |
| 54 | + operation.VisitDolphin(this); |
56 | 55 | } |
| 56 | +} |
57 | 57 |
|
58 | | - class Speak : IAnimalOperation |
| 58 | +class Speak : IAnimalOperation |
| 59 | +{ |
| 60 | + public void VisitDolphin(Dolphin dolphin) |
59 | 61 | { |
60 | | - public void VisitDolphin(Dolphin dolphin) |
61 | | - { |
62 | | - dolphin.Speak(); |
63 | | - } |
| 62 | + dolphin.Speak(); |
| 63 | + } |
64 | 64 |
|
65 | | - public void VisitLion(Lion lion) |
66 | | - { |
67 | | - lion.Roar(); |
68 | | - } |
| 65 | + public void VisitLion(Lion lion) |
| 66 | + { |
| 67 | + lion.Roar(); |
| 68 | + } |
69 | 69 |
|
70 | | - public void VisitMonkey(Monkey monkey) |
71 | | - { |
72 | | - monkey.Shout(); |
73 | | - } |
| 70 | + public void VisitMonkey(Monkey monkey) |
| 71 | + { |
| 72 | + monkey.Shout(); |
74 | 73 | } |
| 74 | +} |
75 | 75 |
|
76 | | - class Jump : IAnimalOperation |
| 76 | +class Jump : IAnimalOperation |
| 77 | +{ |
| 78 | + public void VisitDolphin(Dolphin dolphin) |
77 | 79 | { |
78 | | - public void VisitDolphin(Dolphin dolphin) |
79 | | - { |
80 | 80 | Console.WriteLine("Walked on water a little and disappeared!"); |
81 | 81 | } |
82 | 82 |
|
83 | | - public void VisitLion(Lion lion) |
84 | | - { |
85 | | - Console.WriteLine("Jumped 7 feet! Back on the ground!"); |
86 | | - } |
| 83 | + public void VisitLion(Lion lion) |
| 84 | + { |
| 85 | + Console.WriteLine("Jumped 7 feet! Back on the ground!"); |
| 86 | + } |
87 | 87 |
|
88 | | - public void VisitMonkey(Monkey monkey) |
89 | | - { |
| 88 | + public void VisitMonkey(Monkey monkey) |
| 89 | + { |
90 | 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