From dd93a7c09946a33738094e6bd2d5f965a38c056a Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Thu, 23 Jun 2016 09:39:23 +0300 Subject: [PATCH] Update Command pattern example by adding more examples using buy/sell stock --- src/BehavioralPatterns/Program.cs | 8 ++-- src/CommandPattern/CommandPatternExamples.cs | 9 +++-- src/CommandPattern/StocksExample/Agent.cs | 4 +- .../StocksExample/IStockSchedule.cs | 11 ++++++ .../StocksExample/StockExampleRunner.cs | 38 ++++++++++++++++++- .../StocksExample/StockSchedule.cs | 27 ++++++++++++- .../IteratorPatternExamples.cs | 2 + 7 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 src/CommandPattern/StocksExample/IStockSchedule.cs diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 976f875..92921e3 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -17,14 +17,14 @@ namespace BehavioralPatterns //Chain of responsibillity //This is usefull when you have a request and you don't know who should process it - ChainOfResponsibillityExamples.Run(); - Console.ReadKey(); + //ChainOfResponsibillityExamples.Run(); + //Console.ReadKey(); CommandPatternExamples.Run(); Console.ReadKey(); - IteratorPatternExamples.Run(); - Console.ReadKey(); + //IteratorPatternExamples.Run(); + //Console.ReadKey(); } } diff --git a/src/CommandPattern/CommandPatternExamples.cs b/src/CommandPattern/CommandPatternExamples.cs index f174510..2f7b724 100644 --- a/src/CommandPattern/CommandPatternExamples.cs +++ b/src/CommandPattern/CommandPatternExamples.cs @@ -15,11 +15,12 @@ namespace CommandPattern StockExampleRunner stockExampleRunner = new StockExampleRunner(); Console.WriteLine(stockExampleRunner.GetDescriptionOfExample()); + GoToNextStep(); - stockExampleRunner.Run(); - stockExampleRunner.Run(); - stockExampleRunner.Run(); - stockExampleRunner.Run(); + stockExampleRunner.RunWithRealStockSchedule(); + stockExampleRunner.RunWithMarketClosed(); + stockExampleRunner.RunWithMarketOpened(); + stockExampleRunner.RunWithMarketOpeningInTwoSeconds(); } private static void GoToNextStep() diff --git a/src/CommandPattern/StocksExample/Agent.cs b/src/CommandPattern/StocksExample/Agent.cs index 5b67170..c20642d 100644 --- a/src/CommandPattern/StocksExample/Agent.cs +++ b/src/CommandPattern/StocksExample/Agent.cs @@ -12,9 +12,9 @@ namespace CommandPattern.StocksExample { Stack ordersNotExecuted; FixedSizedQueue ordersPlaced; - StockSchedule stockSchedule; + IStockSchedule stockSchedule; - public Agent(StockSchedule stockSchedule) + public Agent(IStockSchedule stockSchedule) { ordersNotExecuted = new Stack(); ordersPlaced = new FixedSizedQueue(10); diff --git a/src/CommandPattern/StocksExample/IStockSchedule.cs b/src/CommandPattern/StocksExample/IStockSchedule.cs new file mode 100644 index 0000000..afe4e26 --- /dev/null +++ b/src/CommandPattern/StocksExample/IStockSchedule.cs @@ -0,0 +1,11 @@ +using System; + +namespace CommandPattern.StocksExample +{ + public interface IStockSchedule + { + event EventHandler StockExchangedOpened; + + bool IsStockOpen(); + } +} \ No newline at end of file diff --git a/src/CommandPattern/StocksExample/StockExampleRunner.cs b/src/CommandPattern/StocksExample/StockExampleRunner.cs index d51efb6..3a4f148 100644 --- a/src/CommandPattern/StocksExample/StockExampleRunner.cs +++ b/src/CommandPattern/StocksExample/StockExampleRunner.cs @@ -8,11 +8,45 @@ namespace CommandPattern.StocksExample { public class StockExampleRunner { - public void Run() + public void RunWithRealStockSchedule() { - StocksAPI stocksAPI = new StocksAPI(); + Console.WriteLine("Buy/Sell stock with real stock schedule"); + Agent agent = new Agent(new StockSchedule()); + BuyAndSellApplStock(agent); + } + + public void RunWithMarketClosed() + { + Console.WriteLine("Buy/Sell stock when the market is closed, and not opening soon"); + + Agent agent = new Agent(new FakeStockSchedule(false, TimeSpan.FromHours(10))); + + BuyAndSellApplStock(agent); + } + + public void RunWithMarketOpened() + { + Console.WriteLine("Buy/Sell stock when the market is opened"); + + Agent agent = new Agent(new FakeStockSchedule(true, TimeSpan.FromHours(10))); + + BuyAndSellApplStock(agent); + } + + public void RunWithMarketOpeningInTwoSeconds() + { + Console.WriteLine("Buy/Sell stock when the market is closed, but opening in 2 seconds"); + + Agent agent = new Agent(new FakeStockSchedule(false, TimeSpan.FromSeconds(2))); + + BuyAndSellApplStock(agent); + } + + private static void BuyAndSellApplStock(Agent agent) + { + StocksAPI stocksAPI = new StocksAPI(); Stock stock = new Stock { Name = "AAPL", Quantity = 20 }; agent.PlaceOrder(new BuyStockOrder(stocksAPI, stock)); diff --git a/src/CommandPattern/StocksExample/StockSchedule.cs b/src/CommandPattern/StocksExample/StockSchedule.cs index e03f06c..587fba3 100644 --- a/src/CommandPattern/StocksExample/StockSchedule.cs +++ b/src/CommandPattern/StocksExample/StockSchedule.cs @@ -3,7 +3,32 @@ using System.Threading; namespace CommandPattern.StocksExample { - public class StockSchedule + public class FakeStockSchedule : IStockSchedule + { + TimeSpan timeWhenMarketOpens; + bool isStockOpen; + public FakeStockSchedule(bool isStockOpen, TimeSpan dueTime) + { + this.isStockOpen = isStockOpen; + if (!isStockOpen) + { + var timer = new Timer((obj) => + { + this.isStockOpen = true; + StockExchangedOpened?.Invoke(this, null); + }, null, Timeout.Infinite, Timeout.Infinite); + + timer.Change((int)dueTime.TotalMilliseconds, Timeout.Infinite); + } + } + public event EventHandler StockExchangedOpened; + + public bool IsStockOpen() + { + return isStockOpen; + } + } + public class StockSchedule : IStockSchedule { TimeSpan openingTime; TimeSpan closingTime; diff --git a/src/IteratorPattern/IteratorPatternExamples.cs b/src/IteratorPattern/IteratorPatternExamples.cs index a3d95b9..2983354 100644 --- a/src/IteratorPattern/IteratorPatternExamples.cs +++ b/src/IteratorPattern/IteratorPatternExamples.cs @@ -18,6 +18,8 @@ namespace IteratorPattern ReadBigFilesExample bigFileExample = new ReadBigFilesExample(); bigFileExample.Run(); + + //Iterator that selects in batches } } }