Skip to content

Commit d3c6a3e

Browse files
committed
visitor pattern changes
1 parent 1411250 commit d3c6a3e

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

VisitorPattern/Program.cs

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,120 @@
22

33
namespace VisitorPattern
44
{
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()
722
{
8-
void Accept(IAnimalOperation operation);
23+
Console.WriteLine("Oooh o aa aa!");
924
}
1025

11-
// Visitor
12-
interface IAnimalOperation
26+
public void Accept(IAnimalOperation operation)
1327
{
14-
void VisitMonkey(Monkey monkey);
15-
void VisitLion(Lion lion);
16-
void VisitDolphin(Dolphin dolphin);
28+
operation.VisitMonkey(this);
1729
}
30+
}
1831

19-
class Monkey : IAnimal
32+
class Lion : IAnimal
33+
{
34+
public void Roar()
2035
{
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!");
3037
}
3138

32-
class Lion : IAnimal
39+
public void Accept(IAnimalOperation operation)
3340
{
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);
4342
}
43+
}
4444

45-
class Dolphin : IAnimal
45+
class Dolphin : IAnimal
46+
{
47+
public void Speak()
4648
{
47-
public void Speak()
48-
{
49-
Console.WriteLine("Tuut tittu tuutt!");
50-
}
49+
Console.WriteLine("Tuut tittu tuutt!");
50+
}
5151

52-
public void Accept(IAnimalOperation operation)
53-
{
54-
throw new NotImplementedException();
55-
}
52+
public void Accept(IAnimalOperation operation)
53+
{
54+
operation.VisitDolphin(this);
5655
}
56+
}
5757

58-
class Speak : IAnimalOperation
58+
class Speak : IAnimalOperation
59+
{
60+
public void VisitDolphin(Dolphin dolphin)
5961
{
60-
public void VisitDolphin(Dolphin dolphin)
61-
{
62-
dolphin.Speak();
63-
}
62+
dolphin.Speak();
63+
}
6464

65-
public void VisitLion(Lion lion)
66-
{
67-
lion.Roar();
68-
}
65+
public void VisitLion(Lion lion)
66+
{
67+
lion.Roar();
68+
}
6969

70-
public void VisitMonkey(Monkey monkey)
71-
{
72-
monkey.Shout();
73-
}
70+
public void VisitMonkey(Monkey monkey)
71+
{
72+
monkey.Shout();
7473
}
74+
}
7575

76-
class Jump : IAnimalOperation
76+
class Jump : IAnimalOperation
77+
{
78+
public void VisitDolphin(Dolphin dolphin)
7779
{
78-
public void VisitDolphin(Dolphin dolphin)
79-
{
8080
Console.WriteLine("Walked on water a little and disappeared!");
8181
}
8282

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+
}
8787

88-
public void VisitMonkey(Monkey monkey)
89-
{
88+
public void VisitMonkey(Monkey monkey)
89+
{
9090
Console.WriteLine("Jumped 20 feet high! on to the tree!");
9191
}
92-
}
92+
}
9393

9494

9595
class Program
9696
{
9797
static void Main(string[] args)
9898
{
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();
102102

103-
var speak = new Speak();
103+
var speak = new Speak();
104104

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!
108108

109-
var jump = new Jump();
109+
var jump = new Jump();
110110

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!
113113

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!
116116

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
119119

120120
Console.ReadLine();
121121
}

0 commit comments

Comments
 (0)