Skip to content

Commit d8ed040

Browse files
committed
add visitor pattern,
behavioral patern is compelted.
1 parent df40821 commit d8ed040

12 files changed

+267
-0
lines changed

Behavioral/Visitor/Body.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Visitor {
2+
3+
class Body : ICarElement {
4+
5+
public void Accept(ICarElementVisitor visitor) {
6+
visitor.Visit(this);
7+
}
8+
9+
}
10+
11+
}

Behavioral/Visitor/Car.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Visitor {
2+
3+
class Car : ICarElement {
4+
5+
private readonly ICarElement[] _elements;
6+
7+
public Car() {
8+
this._elements = new ICarElement[] {
9+
new Wheel("front left"),
10+
new Wheel("front right"),
11+
new Wheel("back left"),
12+
new Wheel("back right"),
13+
new Body(),
14+
new Engine()
15+
};
16+
}
17+
18+
public void Accept(ICarElementVisitor visitor) {
19+
foreach (var element in this._elements) {
20+
element.Accept(visitor);
21+
}
22+
visitor.Visit(this);
23+
}
24+
}
25+
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace Visitor {
4+
5+
class CarElementDoVisitor : ICarElementVisitor {
6+
7+
public void Visit(Wheel wheel) {
8+
Console.WriteLine("kicking my {0} wheel", wheel.Name);
9+
}
10+
11+
public void Visit(Engine engine) {
12+
Console.WriteLine("starting my engine");
13+
}
14+
15+
public void Visit(Body body) {
16+
Console.WriteLine("moving my body");
17+
}
18+
19+
public void Visit(Car car) {
20+
Console.WriteLine("starting my car");
21+
}
22+
}
23+
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace Visitor {
4+
5+
class CarElementPrintVisitor : ICarElementVisitor {
6+
7+
public void Visit(Wheel wheel) {
8+
Console.WriteLine("visiting {0} wheel", wheel.Name);
9+
}
10+
11+
public void Visit(Engine engine) {
12+
Console.WriteLine("visiting engine");
13+
}
14+
15+
public void Visit(Body body) {
16+
Console.WriteLine("visiting body");
17+
}
18+
19+
public void Visit(Car car) {
20+
Console.WriteLine("visitig car");
21+
}
22+
23+
}
24+
25+
}

Behavioral/Visitor/Engine.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Visitor {
2+
3+
class Engine : ICarElement {
4+
5+
public void Accept(ICarElementVisitor visitor) {
6+
visitor.Visit(this);
7+
}
8+
9+
}
10+
11+
}

Behavioral/Visitor/ICarElement.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Visitor {
2+
3+
interface ICarElement {
4+
5+
void Accept(ICarElementVisitor visitor);
6+
7+
}
8+
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Visitor {
2+
3+
interface ICarElementVisitor {
4+
5+
void Visit(Wheel wheel);
6+
7+
void Visit(Engine engine);
8+
9+
void Visit(Body body);
10+
11+
void Visit(Car car);
12+
13+
}
14+
15+
}

Behavioral/Visitor/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace Visitor {
4+
5+
class Program {
6+
7+
static void Main(string[] args) {
8+
9+
var car = new Car();
10+
car.Accept(new CarElementPrintVisitor());
11+
car.Accept(new CarElementDoVisitor());
12+
13+
Console.ReadKey();
14+
}
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Visitor")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Visitor")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("70e3f33b-8ee0-4c32-9aef-6d4576a7d07d")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Behavioral/Visitor/Visitor.csproj

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Visitor</RootNamespace>
12+
<AssemblyName>Visitor</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Body.cs" />
47+
<Compile Include="Car.cs" />
48+
<Compile Include="CarElementDoVisitor.cs" />
49+
<Compile Include="CarElementPrintVisitor.cs" />
50+
<Compile Include="Engine.cs" />
51+
<Compile Include="ICarElement.cs" />
52+
<Compile Include="ICarElementVisitor.cs" />
53+
<Compile Include="Program.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
<Compile Include="Wheel.cs" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>

Behavioral/Visitor/Wheel.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Visitor {
2+
3+
class Wheel : ICarElement {
4+
5+
public string Name {
6+
get;
7+
private set;
8+
}
9+
10+
public Wheel(string name) {
11+
this.Name = name;
12+
}
13+
14+
public void Accept(ICarElementVisitor visitor) {
15+
visitor.Visit(this);
16+
}
17+
18+
}
19+
20+
}

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Behavioral\Stra
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplateMethod", "Behavioral\TemplateMethod\TemplateMethod.csproj", "{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Visitor", "Behavioral\Visitor\Visitor.csproj", "{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -93,6 +95,12 @@ Global
9395
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|Any CPU.ActiveCfg = Release|x86
9496
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|x86.ActiveCfg = Release|x86
9597
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|x86.Build.0 = Release|x86
98+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Debug|Any CPU.ActiveCfg = Debug|x86
99+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Debug|x86.ActiveCfg = Debug|x86
100+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Debug|x86.Build.0 = Debug|x86
101+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Release|Any CPU.ActiveCfg = Release|x86
102+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Release|x86.ActiveCfg = Release|x86
103+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32}.Release|x86.Build.0 = Release|x86
96104
EndGlobalSection
97105
GlobalSection(SolutionProperties) = preSolution
98106
HideSolutionNode = FALSE
@@ -108,5 +116,6 @@ Global
108116
{F214B483-E8BB-477D-807F-24F176B588D5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
109117
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
110118
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
119+
{3A2CEFD2-07E1-41C1-B52F-F1801D277E32} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
111120
EndGlobalSection
112121
EndGlobal

0 commit comments

Comments
 (0)