Skip to content

Commit

Permalink
add "chain of responsibility" pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
beginor committed Jan 18, 2012
0 parents commit 7e94d49
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.suo
*.manifest
*.user
*.pidb
*.DS_Store
TestResults
Debug/
Release/
obj/
bin/
Bin/
ClientBin/
clientbin/
93 changes: 93 additions & 0 deletions Behavioral/ChainOfResponsibility/ChainOfResponsibility.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ChainOfResponsibility</RootNamespace>
<AssemblyName>ChainOfResponsibility</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisLogFile>bin\Debug\ChainOfResponsibility.exe.CodeAnalysisLog.xml</CodeAnalysisLogFile>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSetDirectories>;D:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisRuleDirectories>;D:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisLogFile>bin\Release\ChainOfResponsibility.exe.CodeAnalysisLog.xml</CodeAnalysisLogFile>
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSetDirectories>;D:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisRuleDirectories>;D:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ConcreteHandler1.cs" />
<Compile Include="ConcreteHandler2.cs" />
<Compile Include="Handler.cs" />
<Compile Include="Client.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Request.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
23 changes: 23 additions & 0 deletions Behavioral/ChainOfResponsibility/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChainOfResponsibility {

class Client {

static void Main(string[] args) {
Handler handlerChain = new ConcreteHandler1(new ConcreteHandler2(null));

Request request1 = new ConcreteRequest1();
handlerChain.HandleRequest(request1);

Request request2 = new ConcreteRequest2();
handlerChain.HandleRequest(request2);

Console.ReadKey();
}
}

}
19 changes: 19 additions & 0 deletions Behavioral/ChainOfResponsibility/ConcreteHandler1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ChainOfResponsibility {

public class ConcreteHandler1 : Handler {

public ConcreteHandler1(Handler successor) : base(successor) {
}

public override void HandleRequest(Request request) {
if (request is ConcreteRequest1) {
Console.WriteLine("ConcreteRequest1 is handled by ConcreteHandler1");
}
else {
this.Successor.HandleRequest(request);
}
}
}
}
19 changes: 19 additions & 0 deletions Behavioral/ChainOfResponsibility/ConcreteHandler2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace ChainOfResponsibility {

public class ConcreteHandler2 : Handler {

public ConcreteHandler2(Handler successor) : base(successor) {
}

public override void HandleRequest(Request request) {
if (request is ConcreteRequest2) {
Console.WriteLine("ConcreteRequest2 is handled by ConcreteHandler2");
}
else {
this.Successor.HandleRequest(request);
}
}
}
}
20 changes: 20 additions & 0 deletions Behavioral/ChainOfResponsibility/Handler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ChainOfResponsibility {

public abstract class Handler {

private readonly Handler _successor;

protected Handler(Handler successor) {
this._successor = successor;
}

protected Handler Successor {
get {
return this._successor;
}
}

public abstract void HandleRequest(Request request);

}
}
36 changes: 36 additions & 0 deletions Behavioral/ChainOfResponsibility/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ChainOfResponsibility")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ChainOfResponsibility")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9e6bee19-8a0a-4b35-9b34-29556b0be99a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
11 changes: 11 additions & 0 deletions Behavioral/ChainOfResponsibility/Request.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace ChainOfResponsibility {

public abstract class Request {
}

public class ConcreteRequest1 : Request {
}

public class ConcreteRequest2 : Request {
}
}
25 changes: 25 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Behavioral", "Behavioral", "{B3C3F0E2-50B6-450B-A161-F4500F4E681D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "Behavioral\ChainOfResponsibility\ChainOfResponsibility.csproj", "{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A291BF92-4BE9-42DB-9414-8E09F04FD3F0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A291BF92-4BE9-42DB-9414-8E09F04FD3F0} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CSharp Design Patterns

0 comments on commit 7e94d49

Please sign in to comment.