Add mediator examples
This commit is contained in:
16
src/MediatorPattern/StockExchange/Buyer.cs
Normal file
16
src/MediatorPattern/StockExchange/Buyer.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
public class Buyer : Trader
|
||||
{
|
||||
IStockExchange stockExchange;
|
||||
public Buyer(string name, string symbol, int count, double price, IStockExchange stockExchange) : base(name, symbol, count, price)
|
||||
{
|
||||
this.stockExchange = stockExchange;
|
||||
}
|
||||
|
||||
public bool BuyFromStockExchange()
|
||||
{
|
||||
return stockExchange.Buy(this, new StockRequest { Count = Count, Price = Price, Symbol = Symbol, Requester = Name });
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/MediatorPattern/StockExchange/IStockExchange.cs
Normal file
17
src/MediatorPattern/StockExchange/IStockExchange.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
/// <summary>
|
||||
/// Mediator
|
||||
/// </summary>
|
||||
public interface IStockExchange
|
||||
{
|
||||
bool Buy(Trader trader, StockRequest request);
|
||||
|
||||
bool Sell(Trader trader, StockRequest request);
|
||||
}
|
||||
}
|
||||
21
src/MediatorPattern/StockExchange/Seller.cs
Normal file
21
src/MediatorPattern/StockExchange/Seller.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
public class Seller : Trader
|
||||
{
|
||||
IStockExchange stockExchange;
|
||||
public Seller(string name, string symbol, int count, double price, IStockExchange stockExchange) : base(name, symbol, count, price)
|
||||
{
|
||||
this.stockExchange = stockExchange;
|
||||
}
|
||||
|
||||
public bool SellFromStockExchange()
|
||||
{
|
||||
return stockExchange.Sell(this, new StockRequest { Count = Count, Price = Price, Symbol = Symbol, Requester = Name });
|
||||
}
|
||||
}
|
||||
}
|
||||
89
src/MediatorPattern/StockExchange/StockExchange.cs
Normal file
89
src/MediatorPattern/StockExchange/StockExchange.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
public class StockExchange : IStockExchange
|
||||
{
|
||||
Dictionary<string, List<Trader>> sellers;
|
||||
Dictionary<string, List<Trader>> buyers;
|
||||
|
||||
public StockExchange()
|
||||
{
|
||||
sellers = new Dictionary<string, List<Trader>>();
|
||||
buyers = new Dictionary<string, List<Trader>>();
|
||||
|
||||
}
|
||||
protected void AddSeller(Trader trader)
|
||||
{
|
||||
AddTrader(trader, sellers);
|
||||
}
|
||||
|
||||
protected void AddBuyer(Trader trader)
|
||||
{
|
||||
AddTrader(trader, buyers);
|
||||
}
|
||||
|
||||
public bool Buy(Trader trader, StockRequest request)
|
||||
{
|
||||
if (!sellers.ContainsKey(request.Symbol))
|
||||
{
|
||||
AddBuyer(trader);
|
||||
Console.WriteLine("There are no sellers for {0}", request.Symbol);
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Trader> sellersOfStock = sellers[request.Symbol];
|
||||
|
||||
var couldBuyStocks = sellersOfStock.Any(s => s.AcceptSell(request));
|
||||
|
||||
if (!couldBuyStocks)
|
||||
{
|
||||
AddBuyer(trader);
|
||||
Console.WriteLine("No one seller sells it at this price, try a higher price");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Actions bought");
|
||||
}
|
||||
|
||||
return couldBuyStocks;
|
||||
}
|
||||
|
||||
public bool Sell(Trader trader, StockRequest request)
|
||||
{
|
||||
if (!buyers.ContainsKey(request.Symbol))
|
||||
{
|
||||
AddSeller(trader);
|
||||
Console.WriteLine("There are no buyers for {0}", request.Symbol);
|
||||
return false;
|
||||
}
|
||||
|
||||
var couldSellStocks = buyers[request.Symbol].Any(b => b.AcceptBuy(request));
|
||||
|
||||
if (!couldSellStocks)
|
||||
{
|
||||
AddSeller(trader);
|
||||
Console.WriteLine("No one seller buys it at this price, try a lower price");
|
||||
}
|
||||
|
||||
|
||||
return couldSellStocks;
|
||||
}
|
||||
|
||||
private void AddTrader(Trader trader, Dictionary<string, List<Trader>> traderList)
|
||||
{
|
||||
if (!traderList.ContainsKey(trader.Symbol))
|
||||
{
|
||||
traderList.Add(trader.Symbol, new List<Trader> { trader });
|
||||
}
|
||||
else
|
||||
{
|
||||
traderList[trader.Symbol].Add(trader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
src/MediatorPattern/StockExchange/StockExchangeExample.cs
Normal file
24
src/MediatorPattern/StockExchange/StockExchangeExample.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
public class StockExchangeExample
|
||||
{
|
||||
public void Run()
|
||||
{
|
||||
IStockExchange stockExchange = new StockExchange();
|
||||
Buyer aaplBuyer = new Buyer("El Ciupi AAPL", "AAPL", 100, 15.0, stockExchange);
|
||||
var buyResult = aaplBuyer.BuyFromStockExchange();
|
||||
|
||||
Seller aaplSeller = new Seller("capo di APPL", "AAPL", 100, 16.0, stockExchange);
|
||||
var sellResult = aaplSeller.SellFromStockExchange();
|
||||
|
||||
aaplSeller = new Seller("capo di APPL", "AAPL", 100, 14.0, stockExchange);
|
||||
sellResult = aaplSeller.SellFromStockExchange();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/MediatorPattern/StockExchange/StockRequest.cs
Normal file
18
src/MediatorPattern/StockExchange/StockRequest.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
public class StockRequest
|
||||
{
|
||||
public string Symbol { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public double Price { get; set; }
|
||||
|
||||
public string Requester { get; set; }
|
||||
}
|
||||
}
|
||||
47
src/MediatorPattern/StockExchange/Trader.cs
Normal file
47
src/MediatorPattern/StockExchange/Trader.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
namespace MediatorPattern.StockExchange
|
||||
{
|
||||
/// <summary>
|
||||
/// Colleague
|
||||
/// </summary>
|
||||
public class Trader
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
public Trader(string name, string symbol, int count, double price)
|
||||
{
|
||||
Name = name;
|
||||
Symbol = symbol;
|
||||
Count = count;
|
||||
Price = price;
|
||||
}
|
||||
public string Symbol { get; private set; }
|
||||
|
||||
public int Count { get; private set; }
|
||||
|
||||
public double Price { get; private set; }
|
||||
|
||||
public virtual bool AcceptSell(StockRequest request)
|
||||
{
|
||||
if(request.Price >= Price)
|
||||
{
|
||||
Console.WriteLine("{0} will sell {1} actions of {2} at the price of {3} to {4}",
|
||||
Name, request.Count, request.Symbol, request.Price, request.Requester);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual bool AcceptBuy(StockRequest request)
|
||||
{
|
||||
if(request.Price <= Price)
|
||||
{
|
||||
Console.WriteLine("{0} will buy {1} actions of {2} at the price of {3} from {4}",
|
||||
Name, request.Count, request.Symbol, request.Price, request.Requester);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user