Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2dad45d

Browse files
chainofResponsibility pattern example
1 parent 0079eef commit 2dad45d

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
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>

‎ChainOfResponsibilityPattern/Program.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
3+
namespace ChainOfResponsibilityPattern
4+
{
5+
abstract class Account
6+
{
7+
private Account mSuccessor;
8+
protected decimal mBalance;
9+
10+
public void SetNext(Account account)
11+
{
12+
mSuccessor = account;
13+
}
14+
15+
public void Pay(decimal amountTopay)
16+
{
17+
if (CanPay(amountTopay))
18+
{
19+
Console.WriteLine("Paid {0:c} using {1}.", amountTopay, this.GetType().Name);
20+
}
21+
else if (this.mSuccessor != null)
22+
{
23+
Console.WriteLine("Cannot pay using {0}. Proceeding..", this.GetType().Name);
24+
mSuccessor.Pay(amountTopay);
25+
}
26+
else
27+
{
28+
throw new Exception("None of the accounts have enough balance");
29+
}
30+
}
31+
private bool CanPay(decimal amount)
32+
{
33+
return mBalance >= amount ? true : false;
34+
}
35+
}
36+
37+
class Bank : Account
38+
{
39+
public Bank(decimal balance)
40+
{
41+
this.mBalance = balance;
42+
}
43+
}
44+
45+
class Paypal : Account
46+
{
47+
public Paypal(decimal balance)
48+
{
49+
this.mBalance = balance;
50+
}
51+
}
52+
53+
class Bitcoin : Account
54+
{
55+
public Bitcoin(decimal balance)
56+
{
57+
this.mBalance = balance;
58+
}
59+
}
60+
class Program
61+
{
62+
static void Main(string[] args)
63+
{
64+
// Let's prepare a chain like below
65+
// $bank->$paypal->$bitcoin
66+
//
67+
// First priority bank
68+
// If bank can't pay then paypal
69+
// If paypal can't pay then bit coin
70+
var bank = new Bank(100); // Bank with balance 100
71+
var paypal = new Paypal(200); // Paypal with balance 200
72+
var bitcoin = new Bitcoin(300); // Bitcoin with balance 300
73+
74+
bank.SetNext(paypal);
75+
paypal.SetNext(bitcoin);
76+
77+
// Let's try to pay using the first priority i.e. bank
78+
bank.Pay(259);
79+
// Output will be
80+
// ==============
81+
// Cannot pay using bank. Proceeding ..
82+
// Cannot pay using paypal. Proceeding ..:
83+
// Paid 259 using Bitcoin!
84+
85+
Console.ReadLine();
86+
}
87+
}
88+
}

‎DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlyweightPattern", "Flyweig
2727
EndProject
2828
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}"
2929
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.csproj", "{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}"
31+
EndProject
3032
Global
3133
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3234
Debug|Any CPU = Debug|Any CPU
@@ -81,6 +83,10 @@ Global
8183
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
8284
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
8385
{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87+
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
89+
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Release|Any CPU.Build.0 = Release|Any CPU
8490
EndGlobalSection
8591
GlobalSection(SolutionProperties) = preSolution
8692
HideSolutionNode = FALSE

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /