diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 7a701ba..216151c 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -27,6 +27,8 @@ namespace BehavioralPatterns Console.Write("4: Command Pattern\r\n"); Console.Write("5: Mediator Pattern\r\n"); Console.Write("6: Memento Pattern\r\n"); + Console.Write("7: State Pattern\r\n"); + Console.Write("8: Strategy Pattern\r\n"); Console.Write("0: exit\r\n>"); var key = Console.ReadKey(); @@ -54,6 +56,12 @@ namespace BehavioralPatterns case '6': MementoPatternExamples.Run(); break; + case '7': + StatePatternExamples.Run(); + break; + case '8': + StrategyPatternExamples.Run(); + break; } if (key.KeyChar == '0') break; diff --git a/src/IteratorPattern/IteratorPatternExamples.cs b/src/IteratorPattern/IteratorPatternExamples.cs index b860192..a45ad35 100644 --- a/src/IteratorPattern/IteratorPatternExamples.cs +++ b/src/IteratorPattern/IteratorPatternExamples.cs @@ -28,7 +28,8 @@ namespace IteratorPattern public static string GetPatternDescription() { return @"Pattern description: -In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. +In object-oriented programming, the iterator pattern is a design pattern in which an iterator +is used to traverse a container and access the container's elements. C# interfaces helpers for Iterator pattern: IEnumerator, IEnumerable, yield for creating IEnumerable Java: Iterator, Iterable"; } diff --git a/src/StrategyPattern/LoopWithException/LoopWithExceptionExample.cs b/src/StrategyPattern/LoopWithException/LoopWithExceptionExample.cs new file mode 100644 index 0000000..0fa2b6f --- /dev/null +++ b/src/StrategyPattern/LoopWithException/LoopWithExceptionExample.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace StrategyPattern.LoopWithException +{ + public class LoopWithExceptionExample + { + public static void Run() + { + LoopUsingException loopUsingException = new LoopUsingException(40, (i) => Console.WriteLine(i)); + loopUsingException.ExecuteForAll(); + } + } + + public class LoopUsingException + { + private Action executeStrategy; + private int repeatCount; + + public LoopUsingException(int repeatCount, Action executeStrategy) + { + this.repeatCount = repeatCount; + this.executeStrategy = executeStrategy; + } + + public void ExecuteForAll(int index = 1) + { + //Stop condition + try + { + var x = 1 / (repeatCount + 1 - index); + } + catch (DivideByZeroException) + { + return; + } + + + executeStrategy(index); + + ExecuteForAll(++index); + } + } +} diff --git a/src/StrategyPattern/StrategyPatternExamples.cs b/src/StrategyPattern/StrategyPatternExamples.cs index 303e1f2..f0c0f85 100644 --- a/src/StrategyPattern/StrategyPatternExamples.cs +++ b/src/StrategyPattern/StrategyPatternExamples.cs @@ -1,4 +1,5 @@ using StrategyPattern.ArrangeInterview; +using StrategyPattern.LoopWithException; using StrategyPattern.ShippingCalculator; using System; using System.Collections.Generic; @@ -14,6 +15,7 @@ namespace StrategyPattern { public static void Run() { + LoopWithExceptionExample.Run(); ArrangeInterviewMotivationalExample.Run(InterviewPersons.Get());