Skip to content

Commit 80dbefd

Browse files
committed
[Chain of Respo] - Add structural code.
1 parent f7ba198 commit 80dbefd

7 files changed

+146
-16
lines changed

Chain of Resp/Chain of Resp.csproj

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,19 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Program.cs" />
45+
<Compile Include="Structural\ConcreteHandler1.cs" />
46+
<Compile Include="Structural\ConcreteHandler2.cs" />
47+
<Compile Include="Structural\ConcreteHandler3.cs" />
48+
<Compile Include="Structural\Handler.cs" />
49+
<Compile Include="Structural\Program.cs" />
4650
<Compile Include="Properties\AssemblyInfo.cs" />
4751
</ItemGroup>
4852
<ItemGroup>
4953
<None Include="App.config" />
5054
</ItemGroup>
55+
<ItemGroup>
56+
<Folder Include="RealWorld\" />
57+
</ItemGroup>
5158
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5259
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5360
Other similar extension points exist, see Microsoft.Common.targets.

Chain of Resp/Program.cs

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace DoFactory.GangOfFour.Chain.Structural
4+
{
5+
/// <summary>
6+
/// The 'ConcreteHandler1' class
7+
/// </summary>
8+
class ConcreteHandler1 : Handler
9+
{
10+
public override void HandleRequest(int request)
11+
{
12+
if (request >= 0 && request < 10)
13+
{
14+
Console.WriteLine("{0} handled request {1}", this.GetType().Name, request);
15+
}
16+
else if (successor != null)
17+
{
18+
successor.HandleRequest(request);
19+
}
20+
}
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
namespace DoFactory.GangOfFour.Chain.Structural
4+
{
5+
/// <summary>
6+
/// The 'ConcreteHandler2' class
7+
/// </summary>
8+
class ConcreteHandler2 : Handler
9+
{
10+
public override void HandleRequest(int request)
11+
{
12+
if (request >= 10 && request < 20)
13+
{
14+
15+
Console.WriteLine("{0} handled request {1}", this.GetType().Name, request);
16+
}
17+
else if (successor != null)
18+
{
19+
20+
successor.HandleRequest(request);
21+
22+
}
23+
}
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace DoFactory.GangOfFour.Chain.Structural
4+
{
5+
/// <summary>
6+
/// The 'ConcreteHandler3' class
7+
/// </summary>
8+
class ConcreteHandler3 : Handler
9+
{
10+
public override void HandleRequest(int request)
11+
{
12+
if (request >= 20 && request < 30)
13+
{
14+
Console.WriteLine("{0} handled request {1}", this.GetType().Name, request);
15+
}
16+
else if (successor != null)
17+
{
18+
successor.HandleRequest(request);
19+
}
20+
}
21+
}
22+
}

Chain of Resp/Structural/Handler.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace DoFactory.GangOfFour.Chain.Structural
2+
{
3+
/// <summary>
4+
/// The 'Handler' abstract class
5+
/// </summary>
6+
abstract class Handler
7+
{
8+
protected Handler successor;
9+
10+
public void SetSuccessor(Handler successor)
11+
{
12+
this.successor = successor;
13+
}
14+
15+
public abstract void HandleRequest(int request);
16+
17+
}
18+
}

Chain of Resp/Structural/Program.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
3+
/// <summary>
4+
/// This structural code demonstrates the Chain of Responsibility pattern in which several linked
5+
/// objects (the Chain) are offered the opportunity to respond to a request or hand it off to the object next in line.
6+
/// </summary>
7+
namespace DoFactory.GangOfFour.Chain.Structural
8+
{
9+
/// <summary>
10+
/// MainApp startup class for Structural
11+
/// Chain of Responsibility Design Pattern.
12+
/// </summary>
13+
class MainApp
14+
{
15+
/// <summary>
16+
/// Entry point into console application.
17+
/// </summary>
18+
static void Main()
19+
{
20+
// Setup Chain of Responsibility
21+
Handler h1 = new ConcreteHandler1();
22+
Handler h2 = new ConcreteHandler2();
23+
Handler h3 = new ConcreteHandler3();
24+
25+
h1.SetSuccessor(h2);
26+
h2.SetSuccessor(h3);
27+
28+
// Generate and process request
29+
int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
30+
31+
foreach (int request in requests)
32+
{
33+
h1.HandleRequest(request);
34+
}
35+
36+
// Wait for user
37+
Console.ReadKey();
38+
}
39+
}
40+
}
41+
42+
//ConcreteHandler1 handled request 2
43+
//ConcreteHandler1 handled request 5
44+
//ConcreteHandler2 handled request 14
45+
//ConcreteHandler3 handled request 22
46+
//ConcreteHandler2 handled request 18
47+
//ConcreteHandler1 handled request 3
48+
//ConcreteHandler3 handled request 27
49+
//ConcreteHandler3 handled request 20

0 commit comments

Comments
 (0)