Skip to content

Commit 117df78

Browse files
committed
Update CoR pattern examples
1 parent cb0dc42 commit 117df78

33 files changed

+171
-217
lines changed

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/Executor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using ChainOfResponsibilityLibrary.ApprovalExample;
2-
using ChainOfResponsibilityLibrary.LoggingExample;
1+
using ChainOfResponsibilityLibrary.LoggingExample;
32
using ChainOfResponsibilityLibrary.PokerExample;
3+
using ChainOfResponsibilityLibrary.PurchaseApprovalExample;
44
using DesignPatternsLibrary.PatternExecutors;
55

66
namespace ChainOfResponsibilityLibrary;
@@ -15,4 +15,4 @@ public override void Execute()
1515
PokerExecutor.Execute();
1616
PurchaseApprovalExecutor.Execute();
1717
}
18-
}
18+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/LoggingExample/Common/Logger.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
public abstract class Logger
44
{
55
protected readonly LogLevel _loggerLevel;
6-
protected Logger _next;
6+
protected Logger? _next;
77

8-
public Logger(LogLevel loggerLevel)
8+
protected Logger(LogLevel loggerLevel)
99
{
1010
_loggerLevel = loggerLevel;
1111
}
@@ -27,4 +27,4 @@ public void Log(LogLevel level, string message)
2727
}
2828

2929
protected abstract void Write(string message);
30-
}
30+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/LoggingExample/Loggers/ConsoleLogger.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public ConsoleLogger()
99
{
1010
}
1111

12-
protected override void Write(string message)
13-
{
14-
Console.WriteLine($"Console:: {message}");
15-
}
16-
}
12+
protected override void Write(string message) => Console.WriteLine($"Console:: {message}");
13+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/LoggingExample/Loggers/FileLogger.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public FileLogger()
99
{
1010
}
1111

12-
protected override void Write(string message)
13-
{
14-
Console.WriteLine($"File:: {message}");
15-
}
16-
}
12+
protected override void Write(string message) => Console.WriteLine($"File:: {message}");
13+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/LoggingExample/Loggers/PriorityLogger.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public PriorityLogger()
99
{
1010
}
1111

12-
protected override void Write(string message)
13-
{
14-
Console.WriteLine($"Priority monitoring logger:: {message}");
15-
}
16-
}
12+
protected override void Write(string message) => Console.WriteLine($"Priority monitoring logger:: {message}");
13+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/LoggingExample/LoggingExecutor.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ public static void Execute()
1212

1313
Logger loggerChain = InitializeLoggerChain();
1414

15-
var infoMessage = "This is information.";
16-
var debugMessage = "This is debug level information.";
17-
var errorMessage = "This is error information.";
18-
19-
loggerChain.Log(LogLevel.Info, infoMessage);
15+
loggerChain.Log(LogLevel.Info, "This is information.");
2016
Console.WriteLine();
2117

22-
loggerChain.Log(LogLevel.Debug, debugMessage);
18+
loggerChain.Log(LogLevel.Debug, "This is debug level information.");
2319
Console.WriteLine();
2420

25-
loggerChain.Log(LogLevel.Error, errorMessage);
21+
loggerChain.Log(LogLevel.Error, "This is error information.");
2622
Console.WriteLine();
2723
}
2824

@@ -38,4 +34,4 @@ private static Logger InitializeLoggerChain()
3834

3935
return consoleLogger;
4036
}
41-
}
37+
}
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33

44
namespace ChainOfResponsibilityLibrary.PokerExample.Categorizers.Common;
55

6-
public abstract class HandCatagorizer
6+
public abstract class HandCategorizer
77
{
8-
protected HandCatagorizer Next { get; private set; }
8+
protected HandCategorizer? Next { get; private set; }
99

10-
public HandCatagorizer RegisterNext(HandCatagorizer next)
10+
public HandCategorizer RegisterNext(HandCategorizer next)
1111
{
1212
Next = next;
1313
return Next;
1414
}
1515

16-
public abstract HandRanking Catagorize(Hand hand);
16+
public abstract HandRanking Categorize(Hand hand);
17+
18+
protected HandRanking CheckNextCategorizer(Hand hand) => Next?.Categorize(hand) ?? HandRanking.HighCard;
1719

1820
protected static bool HasNOfKind(int n, Hand hand)
1921
{
20-
Dictionary<Value, int> seen = new Dictionary<Value, int>();
22+
var seen = new Dictionary<Value, int>();
2123

22-
foreach (Card card in hand.Cards)
24+
foreach (var card in hand.Cards)
2325
{
2426
if (seen.ContainsKey(card.Value))
2527
{
@@ -31,20 +33,12 @@ protected static bool HasNOfKind(int n, Hand hand)
3133
}
3234
}
3335

34-
foreach (int count in seen.Values)
35-
{
36-
if (count == n)
37-
{
38-
return true;
39-
}
40-
}
41-
42-
return false;
36+
return seen.Values.Any(count => count == n);
4337
}
4438

4539
protected static bool HasStraight(Hand hand)
4640
{
47-
List<Value> values = hand.Cards.Select(card => card.Value).ToList();
41+
var values = hand.Cards.Select(card => card.Value).ToList();
4842
values.Sort();
4943

5044
var expectedValue = (int)values.First();
@@ -64,11 +58,10 @@ protected static bool HasStraight(Hand hand)
6458

6559
protected static bool HasFlush(Hand hand)
6660
{
67-
List<Suit> suits = hand.Cards.Select(card => card.Suit).ToList();
61+
var suits = hand.Cards.Select(card => card.Suit).ToList();
6862
suits.Sort();
6963

70-
Suit expectedSuit = suits.First();
71-
64+
var expectedSuit = suits.First();
7265
return suits.All(suit => suit == expectedSuit);
7366
}
74-
}
67+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/PokerExample/Categorizers/FlushCategorizer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace ChainOfResponsibilityLibrary.PokerExample.Categorizers;
66

7-
public class FlushCategorizer : HandCatagorizer
7+
public class FlushCategorizer : HandCategorizer
88
{
9-
public override HandRanking Catagorize(Hand hand)
9+
public override HandRanking Categorize(Hand hand)
1010
{
1111
if (HasFlush(hand))
1212
{
1313
return HandRanking.Flush;
1414
}
1515

16-
return Next.Catagorize(hand);
16+
return CheckNextCategorizer(hand);
1717
}
18-
}
18+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/PokerExample/Categorizers/FourOfAKindCategorizer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace ChainOfResponsibilityLibrary.PokerExample.Categorizers;
66

7-
public class FourOfAKindCategorizer : HandCatagorizer
7+
public class FourOfAKindCategorizer : HandCategorizer
88
{
9-
public override HandRanking Catagorize(Hand hand)
9+
public override HandRanking Categorize(Hand hand)
1010
{
1111
if (HasNOfKind(4, hand))
1212
{
1313
return HandRanking.FourOfAKind;
1414
}
1515

16-
return Next.Catagorize(hand);
16+
return CheckNextCategorizer(hand);
1717
}
18-
}
18+
}

src/BehavioralPatterns/ChainOfResponsibility/ChainOfResponsibilityLibrary/PokerExample/Categorizers/FullHouseCategorizer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace ChainOfResponsibilityLibrary.PokerExample.Categorizers;
66

7-
public class FullHouseCategorizer : HandCatagorizer
7+
public class FullHouseCategorizer : HandCategorizer
88
{
9-
public override HandRanking Catagorize(Hand hand)
9+
public override HandRanking Categorize(Hand hand)
1010
{
1111
var seen = new Dictionary<Value, int>();
1212

13-
foreach (Card card in hand.Cards)
13+
foreach (var card in hand.Cards)
1414
{
1515
if (seen.ContainsKey(card.Value))
1616
{
@@ -30,6 +30,6 @@ public override HandRanking Catagorize(Hand hand)
3030
}
3131
}
3232

33-
return Next.Catagorize(hand);
33+
return CheckNextCategorizer(hand);
3434
}
35-
}
35+
}

0 commit comments

Comments
 (0)