Skip to content

Commit dc36677

Browse files
committed
visitor pattern example
1 parent 1300f65 commit dc36677

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MementoPattern", "Memento\M
3939
EndProject
4040
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObserverPattern", "ObserverPattern\ObserverPattern.csproj", "{88754C6C-4DA4-4C4B-8E53-4DDD9425A387}"
4141
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisitorPattern", "VisitorPattern\VisitorPattern.csproj", "{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}"
43+
EndProject
4244
Global
4345
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4446
Debug|Any CPU = Debug|Any CPU
@@ -117,6 +119,10 @@ Global
117119
{88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Debug|Any CPU.Build.0 = Debug|Any CPU
118120
{88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Release|Any CPU.ActiveCfg = Release|Any CPU
119121
{88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123+
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
124+
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
125+
{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Release|Any CPU.Build.0 = Release|Any CPU
120126
EndGlobalSection
121127
GlobalSection(SolutionProperties) = preSolution
122128
HideSolutionNode = FALSE

VisitorPattern/Program.cs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

VisitorPattern/VisitorPattern.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)