Update Command pattern example by adding more examples using buy/sell stock
This commit is contained in:
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -12,9 +12,9 @@ namespace CommandPattern.StocksExample
|
||||
{
|
||||
Stack<Order> ordersNotExecuted;
|
||||
FixedSizedQueue<Order> ordersPlaced;
|
||||
StockSchedule stockSchedule;
|
||||
IStockSchedule stockSchedule;
|
||||
|
||||
public Agent(StockSchedule stockSchedule)
|
||||
public Agent(IStockSchedule stockSchedule)
|
||||
{
|
||||
ordersNotExecuted = new Stack<Order>();
|
||||
ordersPlaced = new FixedSizedQueue<Order>(10);
|
||||
|
||||
11
src/CommandPattern/StocksExample/IStockSchedule.cs
Normal file
11
src/CommandPattern/StocksExample/IStockSchedule.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace CommandPattern.StocksExample
|
||||
{
|
||||
public interface IStockSchedule
|
||||
{
|
||||
event EventHandler StockExchangedOpened;
|
||||
|
||||
bool IsStockOpen();
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -18,6 +18,8 @@ namespace IteratorPattern
|
||||
|
||||
ReadBigFilesExample bigFileExample = new ReadBigFilesExample();
|
||||
bigFileExample.Run();
|
||||
|
||||
//Iterator that selects in batches
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user