Skip to content

Commit 141b32b

Browse files
committed
add Chain Of Responsibility pattern example
1 parent 96b2392 commit 141b32b

7 files changed

+369
-3
lines changed

Assembly-CSharp.csproj

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>10.0.20506</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<RootNamespace></RootNamespace>
9+
<ProjectGuid>{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}</ProjectGuid>
10+
<OutputType>Library</OutputType>
11+
<AppDesignerFolder>Properties</AppDesignerFolder>
12+
<AssemblyName>Assembly-CSharp</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<BaseDirectory>Assets</BaseDirectory>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>Temp\bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE;UNITY_5_2_1;UNITY_5_2;UNITY_5;ENABLE_NEW_BUGREPORTER;ENABLE_2D_PHYSICS;ENABLE_4_6_FEATURES;ENABLE_AUDIO;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_DUCK_TYPING;ENABLE_FRAME_DEBUGGER;ENABLE_GENERICS;ENABLE_HOME_SCREEN;ENABLE_IMAGEEFFECTS;ENABLE_LIGHT_PROBES_LEGACY;ENABLE_MICROPHONE;ENABLE_MULTIPLE_DISPLAYS;ENABLE_PHYSICS;ENABLE_PLUGIN_INSPECTOR;ENABLE_SHADOWS;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_SPRITES;ENABLE_TERRAIN;ENABLE_RAKNET;ENABLE_UNET;ENABLE_UNITYEVENTS;ENABLE_WEBCAM;ENABLE_WWW;ENABLE_CLOUD_SERVICES;ENABLE_CLOUD_SERVICES_ADS;ENABLE_CLOUD_HUB;ENABLE_CLOUD_PROJECT_ID;ENABLE_CLOUD_SERVICES_ANALYTICS;ENABLE_CLOUD_SERVICES_UNET;ENABLE_CLOUD_SERVICES_BUILD;ENABLE_CLOUD_LICENSE;ENABLE_EDITOR_METRICS;ENABLE_REFLECTION_BUFFERS;INCLUDE_DYNAMIC_GI;INCLUDE_GI;INCLUDE_IL2CPP;INCLUDE_DIRECTX12;PLATFORM_SUPPORTS_MONO;RENDER_SOFTWARE_CURSOR;ENABLE_LOCALIZATION;ENABLE_ANDROID_ATLAS_ETC1_COMPRESSION;UNITY_WEBPLAYER;ENABLE_SUBSTANCE;WEBPLUG;ENABLE_TEXTUREID_MAP;ENABLE_RUNTIME_GI;ENABLE_MOVIES;ENABLE_NETWORK;ENABLE_CRUNCH_TEXTURE_COMPRESSION;ENABLE_MONO;ENABLE_PROFILER;UNITY_ASSERTIONS;UNITY_EDITOR;UNITY_EDITOR_64;UNITY_EDITOR_WIN;UNITY_TEAM_LICENSE;UNITY_PRO_LICENSE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
<NoWarn>0169</NoWarn>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>Temp\bin\Release\</OutputPath>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
<NoWarn>0169</NoWarn>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.XML" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="UnityEngine">
41+
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/Managed/UnityEngine.dll</HintPath>
42+
</Reference>
43+
<Reference Include="UnityEditor">
44+
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/Managed/UnityEditor.dll</HintPath>
45+
</Reference>
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="Assets\Behavioral Patterns\Chain of Responsibility Pattern\Example1\ChainOfResponsibilityExample1.cs" />
49+
<Compile Include="Assets\Behavioral Patterns\Chain of Responsibility Pattern\Example2\ChainOfResponsibilityExample2.cs" />
50+
<Compile Include="Assets\Behavioral Patterns\Chain of Responsibility Pattern\Structure\ChainOfResponsibilityStructure.cs" />
51+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example1\CommandExample1.cs" />
52+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\DeviceButton.cs" />
53+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\ICommand.cs" />
54+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\IElectronicDevice.cs" />
55+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\Radio.cs" />
56+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TVRemove.cs" />
57+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\Television.cs" />
58+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TestCommandPattern.cs" />
59+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TurnItAllOff.cs" />
60+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TurnTVOff.cs" />
61+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TurnTVOn.cs" />
62+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TurnVolumeDown.cs" />
63+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example2\TurnVolumeUp.cs" />
64+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example3\Command.cs" />
65+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example3\InputHandler.cs" />
66+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example3\MoveCommand.cs" />
67+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Example3\MoveCommandReceiver.cs" />
68+
<Compile Include="Assets\Behavioral Patterns\Command Pattern\Structure\CommandStructure.cs" />
69+
<Compile Include="Assets\Behavioral Patterns\Interpreter Pattern\Example1\InterpreterExample1.cs" />
70+
<Compile Include="Assets\Behavioral Patterns\Interpreter Pattern\Structure\InterpreterStructrue.cs" />
71+
<Compile Include="Assets\Behavioral Patterns\Iterator Pattern\Example1\IteratorExample1.cs" />
72+
<Compile Include="Assets\Behavioral Patterns\Iterator Pattern\Structure\IteratorStructure.cs" />
73+
<Compile Include="Assets\Behavioral Patterns\Mediator Pattern\Example1\MediatorExample1.cs" />
74+
<Compile Include="Assets\Behavioral Patterns\Mediator Pattern\Structure\MediatorStructure.cs" />
75+
<Compile Include="Assets\Behavioral Patterns\Memento Pattern\Example1\MementoExample1.cs" />
76+
<Compile Include="Assets\Behavioral Patterns\Memento Pattern\Structure\MementoStructure.cs" />
77+
<Compile Include="Assets\Behavioral Patterns\Observer Pattern\Example1\ObserverExample1.cs" />
78+
<Compile Include="Assets\Behavioral Patterns\Observer Pattern\Structure\ObserverStructure.cs" />
79+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple1\StateExample1.cs" />
80+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple2\StateExmaple2.cs" />
81+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\ATMMachine.cs" />
82+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\ATMState.cs" />
83+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\HasCard.cs" />
84+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\HasPin.cs" />
85+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\NoCard.cs" />
86+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\NoCash.cs" />
87+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple3\TestATMMachine.cs" />
88+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\DrivingState.cs" />
89+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\DuckingState.cs" />
90+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\Heroine.cs" />
91+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\HeroineBaseState.cs" />
92+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\JumpingState.cs" />
93+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\StandingState.cs" />
94+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Exmaple4\TestHeroine.cs" />
95+
<Compile Include="Assets\Behavioral Patterns\State Pattern\Structure\StateStructure.cs" />
96+
<Compile Include="Assets\Behavioral Patterns\Strategy Pattern\Exmaple1\StrategyPatternExample1.cs" />
97+
<Compile Include="Assets\Behavioral Patterns\Strategy Pattern\Structure\StrategyStructure.cs" />
98+
<Compile Include="Assets\Behavioral Patterns\Template Method Pattern\Exmaple1\TemplateMethodPatternExample1.cs" />
99+
<Compile Include="Assets\Behavioral Patterns\Template Method Pattern\Structure\TemplateMethodStructure.cs" />
100+
<Compile Include="Assets\Behavioral Patterns\Visitor Pattern\Exmaple1\VisitorPatternExample1.cs" />
101+
<Compile Include="Assets\Behavioral Patterns\Visitor Pattern\Structure\VisitorStructure.cs" />
102+
<Compile Include="Assets\Creational Patterns\Abstract Factory Pattern\Example1\AbstractFactoryPatternExample1.cs" />
103+
<Compile Include="Assets\Creational Patterns\Abstract Factory Pattern\Structure\AbstractFactoryStructrue.cs" />
104+
<Compile Include="Assets\Creational Patterns\Builder Pattern\Example1\BuilderPatternExample1.cs" />
105+
<Compile Include="Assets\Creational Patterns\Builder Pattern\Structure\BuilderStructure.cs" />
106+
<Compile Include="Assets\Creational Patterns\Factory Method Pattern\Example1\FactoryMethodPatternExample1.cs" />
107+
<Compile Include="Assets\Creational Patterns\Factory Method Pattern\Structure\FactoryMethodStructure.cs" />
108+
<Compile Include="Assets\Creational Patterns\Prototype Pattern\Example1\PrototypePatternExample1.cs" />
109+
<Compile Include="Assets\Creational Patterns\Prototype Pattern\Structure\PrototypeStructure.cs" />
110+
<Compile Include="Assets\Creational Patterns\Singleton Pattern\Example1\SingletonPatternExample1.cs" />
111+
<Compile Include="Assets\Creational Patterns\Singleton Pattern\Structure\SingletonStructure.cs" />
112+
<Compile Include="Assets\Game Programming Patterns\SubclassSandbox Pattern\example\FlashSpeed.cs" />
113+
<Compile Include="Assets\Game Programming Patterns\SubclassSandbox Pattern\example\GroundDive.cs" />
114+
<Compile Include="Assets\Game Programming Patterns\SubclassSandbox Pattern\example\SkyLaunch.cs" />
115+
<Compile Include="Assets\Game Programming Patterns\SubclassSandbox Pattern\example\SuperPower.cs" />
116+
<Compile Include="Assets\Game Programming Patterns\SubclassSandbox Pattern\example\TestSubclassSandbox.cs" />
117+
<Compile Include="Assets\Structural Patterns\Adapter Pattern\Exmaple1\AdapterPatternExample1.cs" />
118+
<Compile Include="Assets\Structural Patterns\Adapter Pattern\Structure\AdapterStructure.cs" />
119+
<Compile Include="Assets\Structural Patterns\Bridge Pattern\Exmaple1\BridgePatternExample1.cs" />
120+
<Compile Include="Assets\Structural Patterns\Bridge Pattern\Structure\BridgeStructure.cs" />
121+
<Compile Include="Assets\Structural Patterns\Composite Pattern\Exmaple1\CompositePatternExample1.cs" />
122+
<Compile Include="Assets\Structural Patterns\Composite Pattern\Structure\CompositeStructure.cs" />
123+
<Compile Include="Assets\Structural Patterns\Decorator Pattern\Exmaple1\DecoratorPatternExample1.cs" />
124+
<Compile Include="Assets\Structural Patterns\Decorator Pattern\Structure\DecoratorStructure.cs" />
125+
<Compile Include="Assets\Structural Patterns\Facade Pattern\Exmaple1\FacadePatternExample1.cs" />
126+
<Compile Include="Assets\Structural Patterns\Facade Pattern\Structure\FacadeStructure.cs" />
127+
<Compile Include="Assets\Structural Patterns\Flyweight Pattern\Exmaple1\FlyweightPatternExample1.cs" />
128+
<Compile Include="Assets\Structural Patterns\Flyweight Pattern\Structure\FlyweightStructure.cs" />
129+
<Compile Include="Assets\Structural Patterns\Proxy Pattern\Exmaple1\ProxyPatternExample1.cs" />
130+
<Compile Include="Assets\Structural Patterns\Proxy Pattern\Structure\ProxyStructure.cs" />
131+
<Reference Include="UnityEngine.UI">
132+
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
133+
</Reference>
134+
<Reference Include="UnityEngine.Networking">
135+
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/UnityExtensions/Unity/Networking/UnityEngine.Networking.dll</HintPath>
136+
</Reference>
137+
<Reference Include="Mono.Cecil">
138+
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/Managed/Mono.Cecil.dll</HintPath>
139+
</Reference>
140+
<Reference Include="UnityEditor.iOS.Extensions.Xcode">
141+
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/PlaybackEngines/iOSSupport/UnityEditor.iOS.Extensions.Xcode.dll</HintPath>
142+
</Reference>
143+
</ItemGroup>
144+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
145+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
146+
Other similar extension points exist, see Microsoft.Common.targets.
147+
<Target Name="BeforeBuild">
148+
</Target>
149+
<Target Name="AfterBuild">
150+
</Target>
151+
-->
152+
153+
</Project>

Assets/Behavioral Patterns/Chain of Responsibility Pattern/Example2.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
//-------------------------------------------------------------------------------------
2+
// ChainOfResponsibilityExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
public class ChainOfResponsibilityExample2 : MonoBehaviour
9+
{
10+
11+
12+
void Start ( )
13+
{
14+
// create calculation objects that get chained to each other in a sec
15+
Chain calc1 = new AddNumbers();
16+
Chain calc2 = new SubstractNumbers();
17+
Chain calc3 = new DivideNumbers();
18+
Chain calc4 = new MultiplyNumbers();
19+
20+
// now chain them to each other
21+
calc1.SetNextChain(calc2);
22+
calc2.SetNextChain(calc3);
23+
calc3.SetNextChain(calc4);
24+
25+
// this is the request that will be passed to a chain object to let them figure out which calculation objects it the right for the request
26+
// the request is here the CalculationType enum we add. so we want this pair of numbers to be added
27+
Numbers myNumbers = new Numbers(3, 5, CalculationType.Add);
28+
calc1.Calculate(myNumbers);
29+
30+
// another example:
31+
Numbers myOtherNumbers = new Numbers(6, 2, CalculationType.Multiply);
32+
calc1.Calculate(myOtherNumbers);
33+
34+
// or pass it to some chain object inbetween which will not work in this case:
35+
Numbers myLastNumbers = new Numbers(12, 3, CalculationType.Substract);
36+
calc3.Calculate(myLastNumbers);
37+
}
38+
39+
40+
// just defining some types of calculation we want to implement
41+
// it is better than passing string values as requests because you don't risk any typos that way :)
42+
public enum CalculationType
43+
{
44+
Add,
45+
Substract,
46+
Divide,
47+
Multiply
48+
};
49+
50+
51+
52+
53+
// We use this object as an example object to be passed to the calculation chain ;-)
54+
// to figure out what we want to do with it (which is stored in CalculationType/calculationWanted)
55+
public class Numbers
56+
{
57+
// some numbers:
58+
public int number1 { get; protected set; }
59+
public int number2 { get; protected set; }
60+
61+
// here we store in this object what we want to do with it to let the chain figure out who is responsible for it ;-)
62+
public CalculationType calculationWanted { get; protected set; }
63+
64+
// constructor:
65+
public Numbers(int num1, int num2, CalculationType calcWanted)
66+
{
67+
this.number1 = num1;
68+
this.number2 = num2;
69+
this.calculationWanted = calcWanted;
70+
}
71+
}
72+
73+
74+
// doesn't need to be called chain of course ;-)
75+
public interface Chain
76+
{
77+
void SetNextChain(Chain nextChain); // to be called when calulcation fails
78+
void Calculate(Numbers numbers); // try to calculate
79+
}
80+
81+
82+
public class AddNumbers : Chain
83+
{
84+
// each chain object stored a private nextInChain object, that gets called when the method calculate fails
85+
protected Chain nextInChain;
86+
87+
public void SetNextChain(Chain nextChain)
88+
{
89+
this.nextInChain = nextChain;
90+
}
91+
92+
public void Calculate(Numbers request)
93+
{
94+
if(request.calculationWanted == CalculationType.Add)
95+
{
96+
Debug.Log("Adding: " + request.number1 + " + " + request.number2 + " = " + (request.number1 + request.number2).ToString());
97+
}
98+
else if(nextInChain != null)
99+
nextInChain.Calculate(request);
100+
else
101+
Debug.Log ("Handling of request failed: " + request.calculationWanted);
102+
}
103+
}
104+
105+
public class SubstractNumbers : Chain
106+
{
107+
protected Chain nextInChain;
108+
109+
public void SetNextChain(Chain nextChain)
110+
{
111+
this.nextInChain = nextChain;
112+
}
113+
114+
public void Calculate(Numbers request)
115+
{
116+
if(request.calculationWanted == CalculationType.Substract)
117+
{
118+
Debug.Log("Substracting: " + request.number1 + " - " + request.number2 + " = " + (request.number1 - request.number2).ToString());
119+
}
120+
else if(nextInChain != null)
121+
nextInChain.Calculate(request);
122+
else
123+
Debug.Log ("Handling of request failed: " + request.calculationWanted);
124+
}
125+
}
126+
127+
public class DivideNumbers : Chain
128+
{
129+
protected Chain nextInChain;
130+
131+
public void SetNextChain(Chain nextChain)
132+
{
133+
this.nextInChain = nextChain;
134+
}
135+
136+
public void Calculate(Numbers request)
137+
{
138+
if(request.calculationWanted == CalculationType.Divide)
139+
{
140+
Debug.Log("Dividing: " + request.number1 + " / " + request.number2 + " = " + (request.number1 / request.number2).ToString());
141+
}
142+
else if(nextInChain != null)
143+
nextInChain.Calculate(request);
144+
else
145+
Debug.Log ("Handling of request failed: " + request.calculationWanted);
146+
}
147+
}
148+
149+
public class MultiplyNumbers : Chain
150+
{
151+
protected Chain nextInChain;
152+
153+
public void SetNextChain(Chain nextChain)
154+
{
155+
this.nextInChain = nextChain;
156+
}
157+
158+
public void Calculate(Numbers request)
159+
{
160+
if(request.calculationWanted == CalculationType.Multiply)
161+
{
162+
Debug.Log("Multiplying: " + request.number1 + " * " + request.number2 + " = " + (request.number1 * request.number2).ToString());
163+
}
164+
else if(nextInChain != null)
165+
nextInChain.Calculate(request);
166+
else
167+
Debug.Log ("Handling of request failed: " + request.calculationWanted);
168+
}
169+
}
170+
171+
}

Assets/Behavioral Patterns/Chain of Responsibility Pattern/Example2/ChainOfResponsibilityExample2.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Behavioral Patterns/Chain of Responsibility Pattern/Example2/ChainOfResponsibilityExample2.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)