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