ze poker hand checker
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>7843b0f8-7530-4859-8bee-43d370e0f0aa</ProjectGuid>
|
||||
<ProjectGuid>e3092ee0-1282-4ab4-9fa2-0338348d8fd1</ProjectGuid>
|
||||
<RootNamespace>BehavioralPatterns</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
using ChainOfResponssibility;
|
||||
using ChainOfResponssibility.PurchaseExample;
|
||||
using CommandPattern;
|
||||
using IteratorPattern;
|
||||
using MediatorPattern;
|
||||
using MememntoPattern;
|
||||
using ObserverPattern;
|
||||
using StatePattern;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -16,41 +17,45 @@ namespace BehavioralPatterns
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
StatePatternExamples.Run();
|
||||
GoToNextStep();
|
||||
//Chain of responsibillity
|
||||
//This is usefull when you have a request and you don't know who should process it
|
||||
ChainOfResponsibillityExamples.Run();
|
||||
GoToNextStep();
|
||||
while (true)
|
||||
{
|
||||
Console.Write("1: Chain of responsibility\r\n");
|
||||
Console.Write("2: Iterator Pattern\r\n");
|
||||
Console.Write("3: Observer Pattern\r\n");
|
||||
Console.Write("4: Command Pattern\r\n");
|
||||
Console.Write("5: Mediator Pattern\r\n");
|
||||
Console.Write("6: Memento Pattern\r\n");
|
||||
Console.Write("0: exit\r\n>");
|
||||
var key = Console.ReadKey();
|
||||
|
||||
CommandPatternExamples.Run();
|
||||
GoToNextStep();
|
||||
Console.Clear();
|
||||
|
||||
IteratorPatternExamples.Run();
|
||||
GoToNextStep();
|
||||
|
||||
IteratorPatternExamples.Run();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
MediatorPatternExamples.Run();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
MementoPatternExamples.Run();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
ObserverPatternExamples.Run();
|
||||
GoToNextStep();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void GoToNextStep()
|
||||
{
|
||||
Console.ReadKey();
|
||||
Console.Clear();
|
||||
switch (key.KeyChar)
|
||||
{
|
||||
case '1':
|
||||
//Chain of responsibillity
|
||||
//This is usefull when you have a request and you don't know who should process it
|
||||
ChainOfResponsibillityExamples.Run();
|
||||
break;
|
||||
case '2':
|
||||
IteratorPatternExamples.Run();
|
||||
break;
|
||||
case '3':
|
||||
ObserverPatternExamples.Run();
|
||||
break;
|
||||
case '4':
|
||||
CommandPatternExamples.Run();
|
||||
break;
|
||||
case '5':
|
||||
MediatorPatternExamples.Run();
|
||||
break;
|
||||
case '6':
|
||||
MementoPatternExamples.Run();
|
||||
break;
|
||||
}
|
||||
if (key.KeyChar == '0')
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,4 @@ using System.Runtime.InteropServices;
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("7843b0f8-7530-4859-8bee-43d370e0f0aa")]
|
||||
[assembly: Guid("e3092ee0-1282-4ab4-9fa2-0338348d8fd1")]
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
{
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
"buildOptions": {
|
||||
"emitEntryPoint": true
|
||||
"emitEntryPoint": true,
|
||||
"copyToOutput": {
|
||||
"include": "../IteratorPattern/FileExample/SampleFiles/**"
|
||||
}
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
@@ -12,10 +15,9 @@
|
||||
"MememntoPattern": "1.0.0-*",
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.0"
|
||||
"version": "1.0.0-rc2-3002702"
|
||||
},
|
||||
"ObserverPattern": "1.0.0-*",
|
||||
"StatePattern": "1.0.0-*"
|
||||
"ObserverPattern": "1.0.0-*"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
90
src/ChainOfResponssibility/Poker/Card.cs
Normal file
90
src/ChainOfResponssibility/Poker/Card.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using ChainOfResponssibility.Validators;
|
||||
using ChainOfResponssibility.Poker.Validators;
|
||||
|
||||
namespace ChainOfResponssibility.Poker
|
||||
{
|
||||
public class Card : IEquatable<Card>
|
||||
{
|
||||
public const string ValidValues = "2,3,4,5,6,7,8,9,10,J,Q,K,A";
|
||||
public const string ValidSuites = "C,H,S,D";
|
||||
|
||||
public string Value { get; private set; }
|
||||
public char Suite { get; private set; }
|
||||
public static ChainValidation<Card> CardValidatorFactory { get; private set; }
|
||||
|
||||
public Card(string value, char suite)
|
||||
{
|
||||
Value = value.ToUpper();
|
||||
Suite = suite.ToString().ToUpper()[0];
|
||||
}
|
||||
|
||||
public static Card Parse(string toParse)
|
||||
{
|
||||
Card c = null;
|
||||
switch (toParse.Length)
|
||||
{
|
||||
case 2:
|
||||
c = new Card(toParse[0].ToString(), toParse[1]);
|
||||
break;
|
||||
case 3:
|
||||
c = new Card(toParse.Substring(0, 2), toParse[2]);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException(string.Format("Must be in format VS where V is one of [{0}] and S is one of [{1}]", ValidValues, ValidSuites), nameof(toParse));
|
||||
}
|
||||
|
||||
ChainValidation<Card> Validation = CardValidationFactory.GetValidator();
|
||||
|
||||
var validationResult = Validation.Validate(c);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
throw validationResult.Exception;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
public int GetIntValue()
|
||||
{
|
||||
int val = 0;
|
||||
if (int.TryParse(Value, out val))
|
||||
return val;
|
||||
else
|
||||
switch (Value)
|
||||
{
|
||||
case "J":
|
||||
return 11;
|
||||
case "Q":
|
||||
return 12;
|
||||
case "K":
|
||||
return 13;
|
||||
case "A":
|
||||
return 14;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public bool Equals(Card other)
|
||||
{
|
||||
if (this.Value == other.Value && this.Suite == other.Suite)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool operator ==(Card c1, Card c2)
|
||||
{
|
||||
return c1.Equals(c2);
|
||||
}
|
||||
|
||||
public static bool operator !=(Card c1, Card c2)
|
||||
{
|
||||
return !c1.Equals(c2);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value + Suite;
|
||||
}
|
||||
}
|
||||
}
|
||||
38
src/ChainOfResponssibility/Poker/PokerGame.cs
Normal file
38
src/ChainOfResponssibility/Poker/PokerGame.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using ChainOfResponssibility.Poker.Validators.HandValidators;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker
|
||||
{
|
||||
public class PokerGame
|
||||
{
|
||||
public void newGame()
|
||||
{
|
||||
Console.WriteLine("!!NOT AN ACTUAL POKER GAME!!{0}{1}{0}{2}{0}{3}{0}{4}{0}{5}", Environment.NewLine,
|
||||
"This tells you what hand you have",
|
||||
"Just enter the 5 cards you have, separated with spaces, and press enter",
|
||||
"Card values: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A",
|
||||
"Card Suites: H, D, C, S",
|
||||
"ex.:\"QH AH JH KH 10H\" (yes that is a royal flush right there)");
|
||||
|
||||
string cards = string.Empty;
|
||||
while (cards.ToLower()!="exit")
|
||||
{
|
||||
Console.Write(">");
|
||||
cards = Console.ReadLine();
|
||||
|
||||
try
|
||||
{
|
||||
var hand = cards.Split(' ').Select(x => Card.Parse(x)).OrderBy(x=>x.GetIntValue()).ToArray();
|
||||
Console.WriteLine(HandValidationFactory.GetValidation().Validate(hand));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Console.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using ChainOfResponssibility.Validators;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators
|
||||
{
|
||||
public class CardSuiteValidation : ChainValidation<Card>
|
||||
{
|
||||
protected override ValidationResult IsValid(Card obj)
|
||||
{
|
||||
if (Card.ValidSuites.Contains(obj.Suite))
|
||||
return ValidationResult.GetValidResult();
|
||||
return ValidationResult.GetInvalidResult(new Exception("Invalid Suite, must be one of " + Card.ValidSuites));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using ChainOfResponssibility.Validators;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators
|
||||
{
|
||||
public class CardValidationFactory
|
||||
{
|
||||
private static ChainValidation<Card> _validator;
|
||||
|
||||
public static ChainValidation<Card> GetValidator()
|
||||
{
|
||||
if (_validator != null)
|
||||
return _validator;
|
||||
|
||||
_validator = new CardSuiteValidation();
|
||||
_validator.SetSuccessor(new CardValueValidation());
|
||||
|
||||
return _validator;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using ChainOfResponssibility.Validators;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators
|
||||
{
|
||||
public class CardValueValidation : ChainValidation<Card>
|
||||
{
|
||||
protected override ValidationResult IsValid(Card obj)
|
||||
{
|
||||
if (Card.ValidValues.Contains(obj.Value))
|
||||
return ValidationResult.GetValidResult();
|
||||
return ValidationResult.GetInvalidResult(new Exception("Invalid Value, must be one of " + Card.ValidValues));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class FlushValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Suite);
|
||||
var flush = gr.FirstOrDefault(x => x.Count() == 5);
|
||||
if (flush != null)
|
||||
return "You have a Flush of " + flush.First().Suite + "s";
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class FourOfAKindValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Value);
|
||||
var four = gr.FirstOrDefault(x => x.Count() == 4);
|
||||
if (four != null)
|
||||
return "You have Four of " + four.First().Value + "s (Four of a kind)";
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class FullHouseValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Value);
|
||||
var pair = gr.FirstOrDefault(x => x.Count() == 2);
|
||||
if (pair != null)
|
||||
{
|
||||
var OhBabyATripple = gr.FirstOrDefault(x => x.Count() == 3);
|
||||
if (OhBabyATripple != null)
|
||||
return "You have a Full House of " + OhBabyATripple.First().Value + "s (3) and " + pair.First().Value + "s (2)";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public abstract class HandValidation
|
||||
{
|
||||
public HandValidation Successor { get; private set; }
|
||||
|
||||
public HandValidation SetSuccessor(HandValidation successor)
|
||||
{
|
||||
return Successor = successor;
|
||||
}
|
||||
|
||||
protected abstract string IsValid(Card[] hand);
|
||||
|
||||
public string Validate(Card[] hand)
|
||||
{
|
||||
string result = IsValid(hand);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(result))
|
||||
return result;
|
||||
|
||||
if (Successor != null)
|
||||
return Successor.Validate(hand);
|
||||
else
|
||||
return result;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class HandValidationFactory
|
||||
{
|
||||
private static HandValidation _cachedValidation;
|
||||
|
||||
public static HandValidation GetValidation()
|
||||
{
|
||||
if (_cachedValidation != null)
|
||||
return _cachedValidation;
|
||||
|
||||
var royalflush = new RoyalFlushValidation();
|
||||
|
||||
var straightflush = new StraightFlushValidation();
|
||||
royalflush.SetSuccessor(straightflush);
|
||||
|
||||
var fourofakind = new FourOfAKindValidation();
|
||||
straightflush.SetSuccessor(fourofakind);
|
||||
|
||||
var fullhouse = new FullHouseValidation();
|
||||
fourofakind.SetSuccessor(fullhouse);
|
||||
|
||||
var flush = new FlushValidation();
|
||||
fullhouse.SetSuccessor(flush);
|
||||
|
||||
var straight = new StraightValidation();
|
||||
flush.SetSuccessor(straight);
|
||||
|
||||
var threeofakind = new ThreeOfAKindValidation();
|
||||
straight.SetSuccessor(threeofakind);
|
||||
|
||||
var twopairs = new TwoPairsValidation();
|
||||
threeofakind.SetSuccessor(twopairs);
|
||||
|
||||
var pairs = new PairValidation();
|
||||
twopairs.SetSuccessor(pairs);
|
||||
|
||||
var high = new HighCardValidation();
|
||||
pairs.SetSuccessor(high);
|
||||
|
||||
_cachedValidation = royalflush;
|
||||
return _cachedValidation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class HighCardValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
return string.Format("You only have high cards, here I ordered them for you: {0}",
|
||||
string.Join(", ", hand.OrderByDescending(x => x.GetIntValue()).Select(x => x.ToString())));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class PairValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Value);
|
||||
var pair = gr.FirstOrDefault(x => x.Count() == 2);
|
||||
if (pair != null)
|
||||
return "You have a pair of " + pair.First().Value + "s";
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class RoyalFlushValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Suite);
|
||||
var flush = gr.FirstOrDefault(x => x.Count() == 5);
|
||||
if (flush != null)
|
||||
{
|
||||
var Value2Index = hand.Select((i, j) => i.GetIntValue() - j).Distinct();
|
||||
if (!Value2Index.Skip(1).Any() && Value2Index.First() == 10)// consecutive and 10, meaning 10, J, Q, K, A
|
||||
return "You have a Royal Flush with " + string.Join(", ", hand.Select(x => x.ToString()));
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class StraightFlushValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Suite);
|
||||
var flush = gr.FirstOrDefault(x => x.Count() == 5);
|
||||
if (flush != null)
|
||||
{
|
||||
var Value2Index = hand.Select((i, j) => i.GetIntValue() - j).Distinct();
|
||||
if (!Value2Index.Skip(1).Any())// consecutive
|
||||
return "You have a Straight Flush with " + string.Join(", ", hand.Select(x => x.ToString()));
|
||||
|
||||
//special case for A,2,3,4,5
|
||||
if (Value2Index.Count() == 2
|
||||
&& Value2Index.First() == 2//for 2,3,4,5
|
||||
&& Value2Index.Last() == 10)//for A
|
||||
return "You have a Straight Flush with " + string.Join(", ", hand.Select(x => x.ToString()));
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class StraightValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var Value2Index = hand.Select((i, j) => i.GetIntValue() - j).Distinct();
|
||||
if (!Value2Index.Skip(1).Any())// consecutive
|
||||
return "You have a Straight with " + string.Join(", ", hand.Select(x => x.ToString()));
|
||||
|
||||
//special case for A,2,3,4,5
|
||||
if (Value2Index.Count() == 2
|
||||
&& Value2Index.First() == 2//for 2,3,4,5
|
||||
&& Value2Index.Last() == 10)//for A
|
||||
return "You have a Straight with " + string.Join(", ", hand.Select(x => x.ToString()));
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class ThreeOfAKindValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Value);
|
||||
var OhBabyATripple = gr.FirstOrDefault(x => x.Count() == 3);
|
||||
if (OhBabyATripple != null)
|
||||
return "You have three of " + OhBabyATripple.First().Value + "s (Three of a kind)";
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace ChainOfResponssibility.Poker.Validators.HandValidators
|
||||
{
|
||||
public class TwoPairsValidation : HandValidation
|
||||
{
|
||||
protected override string IsValid(Card[] hand)
|
||||
{
|
||||
var gr = hand.GroupBy(x => x.Value);
|
||||
var pairs = gr.Where(x => x.Count() == 2);
|
||||
if (pairs != null && pairs.Count() == 2)
|
||||
return "You have pairs of " + string.Join(" and ", pairs.Select(x=>x.First().Value + "s"))+ " (Two Pairs)";
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using ChainOfResponssibility.PurchaseExample;
|
||||
using ChainOfResponssibility.Poker;
|
||||
using ChainOfResponssibility.PurchaseExample;
|
||||
using ChainOfResponssibility.TransferFileExample;
|
||||
using ChainOfResponssibility.Validators.UserEntities;
|
||||
using System;
|
||||
@@ -12,40 +13,63 @@ namespace ChainOfResponssibility
|
||||
Console.WriteLine(GetPatternDescription());
|
||||
GoToNextStep();
|
||||
|
||||
Console.WriteLine(ExeucteFirstWhenConditionMatchesFlavorDescription());
|
||||
GoToNextStep();
|
||||
while (true)
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine(ExeucteFirstWhenConditionMatchesFlavorDescription());
|
||||
Console.WriteLine(ExecuteAllUntilConditionIsFalseFlavorDescription());
|
||||
Console.WriteLine(ExecuteAllFlavorDescritpion());
|
||||
Console.WriteLine("1: Money Spender (flavor 1)");
|
||||
Console.WriteLine("2: File Transfer (flavor 1)");
|
||||
Console.WriteLine("3: User Processor (flavor 1 and 3)");
|
||||
Console.WriteLine("4: pitfals");
|
||||
Console.WriteLine("5: Poker Game (flavor 1)");
|
||||
Console.Write("0: exit\r\n>");
|
||||
|
||||
CheckAuthority moneySpender = new CheckAuthority();
|
||||
var keyChar = GoToNextStep();
|
||||
|
||||
Console.WriteLine(moneySpender.GetDescriptionOfExample());
|
||||
GoToNextStep();
|
||||
switch (keyChar)
|
||||
{
|
||||
case '1':
|
||||
CheckAuthority moneySpender = new CheckAuthority();
|
||||
|
||||
moneySpender.PrintHowMuchEachCanSpend();
|
||||
moneySpender.SpendMoney();
|
||||
GoToNextStep();
|
||||
Console.WriteLine(moneySpender.GetDescriptionOfExample());
|
||||
GoToNextStep();
|
||||
|
||||
TransferFilesManager transferFilesManager = new TransferFilesManager();
|
||||
moneySpender.PrintHowMuchEachCanSpend();
|
||||
moneySpender.SpendMoney();
|
||||
break;
|
||||
case '2':
|
||||
TransferFilesManager transferFilesManager = new TransferFilesManager();
|
||||
|
||||
Console.WriteLine(transferFilesManager.GetDescriptionOfExample());
|
||||
GoToNextStep();
|
||||
transferFilesManager.TransferFiles();
|
||||
Console.WriteLine(transferFilesManager.GetDescriptionOfExample());
|
||||
GoToNextStep();
|
||||
transferFilesManager.TransferFiles();
|
||||
break;
|
||||
case '3':
|
||||
UserProcessor userProcessor = new UserProcessor();
|
||||
userProcessor.DoStuff();
|
||||
break;
|
||||
case '4':
|
||||
Console.WriteLine(GetPitfalls());
|
||||
GoToNextStep();
|
||||
break;
|
||||
case '5':
|
||||
PokerGame poke = new PokerGame();
|
||||
poke.newGame();
|
||||
break;
|
||||
}
|
||||
|
||||
GoToNextStep();
|
||||
Console.WriteLine(ExecuteAllUntilConditionIsFalseFlavorDescription());
|
||||
Console.WriteLine(ExecuteAllFlavorDescritpion());
|
||||
GoToNextStep();
|
||||
|
||||
UserProcessor userProcessor = new UserProcessor();
|
||||
userProcessor.DoStuff();
|
||||
|
||||
GoToNextStep();
|
||||
Console.WriteLine(GetPitfalls());
|
||||
if (keyChar == '0')
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void GoToNextStep()
|
||||
private static char GoToNextStep()
|
||||
{
|
||||
Console.ReadKey();
|
||||
var ch = Console.ReadKey().KeyChar;
|
||||
Console.Clear();
|
||||
return ch;
|
||||
}
|
||||
|
||||
public static string GetPatternDescription()
|
||||
@@ -55,9 +79,9 @@ Decouples sender and receiver (as a sender you don't know who will handle the re
|
||||
Hierarchical in nature
|
||||
When using the Chain of Responsibility is more effective:
|
||||
More than one object can handle a command
|
||||
The handler is not known in advances
|
||||
The handler is not known in advance
|
||||
The handler should be determined automatically
|
||||
It's wished that the request is addressed to a group of objects without explicitly specifying its receiver
|
||||
It’s wished that the request is addressed to a group of objects without explicitly specifying its receiver
|
||||
The group of objects that may handle the command must be specified in a dynamic way.
|
||||
Examples in real life:
|
||||
-java.util.logging.Logger.#log()
|
||||
@@ -80,12 +104,12 @@ Chain length/performance issues - in theory you could see a chain that is too bi
|
||||
|
||||
public static string ExecuteAllUntilConditionIsFalseFlavorDescription()
|
||||
{
|
||||
return @"Flavor 2 of chain of responssibility:Execute all elements of chain until the condition does not match";
|
||||
return @"Flavor 2: Execute all elements of chain until the condition does not match";
|
||||
}
|
||||
|
||||
public static string ExecuteAllFlavorDescritpion()
|
||||
{
|
||||
return @"Flavor 3 of chain of responssibility: Execute all elements of chain";
|
||||
return @"Flavor 3: Execute all elements of chain";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,15 +9,15 @@ namespace ChainOfResponssibility.Validators.UserEntities
|
||||
{
|
||||
UserRepository userRepository;
|
||||
PrincipalHelper principalHelper;
|
||||
Operation inputOperation;
|
||||
Operation operation;
|
||||
ChainValidation<User> userCreationValidation;
|
||||
ChainValidation<User> authenticateUserValidation;
|
||||
public UserProcessor()
|
||||
{
|
||||
userRepository = new UserRepository();
|
||||
principalHelper = new PrincipalHelper();
|
||||
inputOperation = new AuthenticateOperation(AuthenticateUser);
|
||||
inputOperation.SetSuccessor(new CreateNewUserOperation(CreateNewUser));
|
||||
operation = new AuthenticateOperation(AuthenticateUser);
|
||||
operation.SetSuccessor(new CreateNewUserOperation(CreateNewUser));
|
||||
|
||||
userCreationValidation = new IsAuthorisedToDoOperationsOnUser(principalHelper, Rights.Create);
|
||||
userCreationValidation.SetSuccessor(new ValidateNoDuplicateEmail(userRepository));
|
||||
@@ -55,12 +55,12 @@ namespace ChainOfResponssibility.Validators.UserEntities
|
||||
string userInput;
|
||||
do
|
||||
{
|
||||
inputOperation.PrintMenu();
|
||||
operation.PrintMenu();
|
||||
Console.Write(">");
|
||||
userInput = Console.ReadLine();
|
||||
|
||||
if (!IsExitCode(userInput))
|
||||
inputOperation.Execute(userInput);
|
||||
operation.Execute(userInput);
|
||||
|
||||
} while (!IsExitCode(userInput));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
@@ -31,6 +31,12 @@ namespace CommandPattern
|
||||
|
||||
}
|
||||
|
||||
private static void GoToNextStep()
|
||||
{
|
||||
Console.ReadKey();
|
||||
Console.Clear();
|
||||
}
|
||||
|
||||
public static string GetPatternDescription()
|
||||
{
|
||||
return @"Patttern description:
|
||||
@@ -70,11 +76,5 @@ level undo/redo functionality, in practice this only works for simple examples,
|
||||
where all entities are at the same level. More details about undo can be seen at:
|
||||
http://gernotklingler.com/blog/implementing-undoredo-with-the-command-pattern/";
|
||||
}
|
||||
|
||||
private static void GoToNextStep()
|
||||
{
|
||||
Console.ReadKey();
|
||||
Console.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
{
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.App": "1.0.0"
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.0-rc2-3002702"
|
||||
}
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.5.0-rc2-24027",
|
||||
"Newtonsoft.Json": "9.0.1",
|
||||
"System.Runtime.Serialization.Primitives": "4.1.1"
|
||||
"Newtonsoft.Json": "8.0.3",
|
||||
"System.Runtime.Serialization.Primitives": "4.0.10-*"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using ObserverPattern.StockUpdateEvents;
|
||||
using ObserverPattern.Twits;
|
||||
using ObserverPattern.Twits;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -12,78 +11,8 @@ namespace ObserverPattern
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
Console.WriteLine(GetDescription());
|
||||
Console.WriteLine(GetActors());
|
||||
Console.WriteLine(IsThisPubSub());
|
||||
|
||||
StockUpdateEventsExample stockExample = new StockUpdateEventsExample();
|
||||
stockExample.RunSimple();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
Console.WriteLine(GetLapsedLinstenerProblem());
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
Console.WriteLine("Same business logic using events combined with RX library");
|
||||
stockExample.RunReactiveWithEvents();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
Console.WriteLine("Same business logic using RX library");
|
||||
stockExample.RunReactive();
|
||||
|
||||
GoToNextStep();
|
||||
|
||||
ObservableTwitsExample obsTwits = new ObservableTwitsExample();
|
||||
obsTwits.Run();
|
||||
|
||||
GoToNextStep();
|
||||
}
|
||||
|
||||
private static string GetDescription()
|
||||
{
|
||||
return @"
|
||||
The observer pattern is a software design pattern in which an object, called the subject,
|
||||
maintains a list of its dependents, called observers, and notifies them automatically of any state changes,
|
||||
usually by calling one of their methods. ";
|
||||
}
|
||||
|
||||
private static string IsThisPubSub()
|
||||
{
|
||||
return @"
|
||||
Observer/Observable pattern is mostly implemented in a synchronous way,
|
||||
i.e. the observable calls the appropriate method of all its observers when some event occurs.
|
||||
The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue).
|
||||
In the Observer/Observable pattern, the observers are aware of the observable.
|
||||
Whereas, in Publisher/Subscriber, publishers and subscribers don't need to know each other.
|
||||
They simply communicate with the help of message queues.
|
||||
";
|
||||
}
|
||||
|
||||
private static string GetActors()
|
||||
{
|
||||
return @"
|
||||
Subject -> Interface/Abstract Notifies interested observers when an event occurs
|
||||
Concrete Subject -> Implementation of Subject
|
||||
Observer -> Interface/Abstract class -> Registers to a subject, to be notified when a specific event happens
|
||||
Concrete Observer -> Implementation of the observer
|
||||
";
|
||||
}
|
||||
|
||||
private static string GetLapsedLinstenerProblem()
|
||||
{
|
||||
return @"
|
||||
The leak happens when a listener fails to unsubscribe from the publisher when it no longer needs to listen.
|
||||
Consequently, the publisher still holds a reference to the observer which prevents it from being garbage collected
|
||||
— including all other objects it is referring to — for as long as the publisher is alive, which could be until the end of the application.
|
||||
This causes not only a memory leak, but also a performance degradation with an 'uninterested' observer receiving and acting on unwanted events";
|
||||
}
|
||||
|
||||
private static void GoToNextStep()
|
||||
{
|
||||
Console.ReadKey();
|
||||
Console.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,16 @@ namespace ObserverPattern.Twits
|
||||
{
|
||||
TwitObservable observable = new TwitObservable();
|
||||
|
||||
using (TwitUser t100 = new TwitUser("t100", observable))
|
||||
using (TwitUser r2d2 = new TwitUser("R2-D2", observable))
|
||||
{
|
||||
t100.Twit("El chupacapra - BOOM BOOM");
|
||||
TwitUser t100 = new TwitUser("t100", observable);
|
||||
TwitUser r2d2 = new TwitUser("R2-D2", observable);
|
||||
|
||||
r2d2.Twit("Vamos vamos mi amor");
|
||||
t100.Twit("El chupacapra - BOOM BOOM");
|
||||
|
||||
observable.ItsGoingHomeTime();
|
||||
}
|
||||
|
||||
r2d2.Twit("Vamos vamos mi amor");
|
||||
|
||||
t100.Dispose();
|
||||
|
||||
observable.ItsGoingHomeTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace ObserverPattern.Twits
|
||||
if (!observers.Contains(observer))
|
||||
observers.Add(observer);
|
||||
|
||||
return new Unsubscriber<Twit>(observers, observer);
|
||||
return new Unsubscriber(observers, observer);
|
||||
}
|
||||
|
||||
public void AddTwit(Twit twit)
|
||||
@@ -37,6 +37,23 @@ namespace ObserverPattern.Twits
|
||||
{
|
||||
observer.OnCompleted();
|
||||
}
|
||||
}
|
||||
|
||||
private class Unsubscriber : IDisposable
|
||||
{
|
||||
private List<IObserver<Twit>> _observers;
|
||||
private IObserver<Twit> _observer;
|
||||
|
||||
public Unsubscriber(List<IObserver<Twit>> observers, IObserver<Twit> observer)
|
||||
{
|
||||
_observers = observers;
|
||||
_observer = observer;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!(_observer == null)) _observers.Remove(_observer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
{
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0",
|
||||
"System.Reactive": "3.0.0"
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
Reference in New Issue
Block a user