using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleSmaple
{
class Program
{
static void Main(string[] args)
{
Commodity aCommodity1 = new Commodity("中华牙膏", 12);
Commodity aCommodity2 = new Commodity("李宁运动服",300);
Console.WriteLine(string.Format("当前中华牙膏的价格:{0}",aCommodity1.Price.ToString()));
Console.WriteLine(string.Format("当前李宁运动服的价格:{0}", aCommodity2.Price.ToString()));
Console.WriteLine();
Buyer aBuyer1 = new Buyer(1);
Buyer aBuyer2 = new Buyer(2);
Subscriber subscriber = new Subscriber();
subscriber.Subscribe(aBuyer1, aCommodity1);
subscriber.Subscribe(aBuyer1, aCommodity2);
subscriber.Subscribe(aBuyer2, aCommodity1);
subscriber.Subscribe(aBuyer2, aCommodity2);
aCommodity1.Price = 15;
Console.WriteLine("取消部分订阅....");
subscriber.CancelSubscribe(aBuyer1, aCommodity2);
subscriber.CancelSubscribe(aBuyer2, aCommodity1);
aCommodity2.Price = 1000;
Console.WriteLine();
Console.WriteLine("Press Enter to Exit");
Console.ReadLine();
}
}
public delegate void ChangePriceEventHandler(object sender, EventArgs e);
/// <summary>
/// 商品类
/// </summary>
public class Commodity
{
private long _price;
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public long Price
{
get { return _price; }
set
{
if (value != _price)
{
_price = value;
OnChangePrice();
}
}
}
public event ChangePriceEventHandler ChangePrice;
public Commodity(string name, long price)
{
_name = name;
_price = price;
}
private void OnChangePrice()
{
ChangePriceEventHandler temp = ChangePrice;
if (temp != null)
{
temp(this, EventArgs.Empty);
}
}
}
/// <summary>
/// 买家类
/// </summary>
public class Buyer
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
public Buyer(int Id)
{
_id = Id;
}
}
/// <summary>
/// 商品集合类
/// </summary>
public class CommodityCollection : List<Commodity>
{
}
/// <summary>
/// 类似一个买家与商品的关系表;
/// </summary>
public class BuyerRefCommodityCollection : Dictionary<Buyer, CommodityCollection>
{
}
/// <summary>
/// 所有用户订阅与取消订阅都在这里实现;
///
/// 如果是多种订阅和取消订阅的方式
/// 可以抽象此类,变为多种策略实现;
/// </summary>
public class Subscriber
{
private BuyerRefCommodityCollection _ref = new BuyerRefCommodityCollection();
private NormalNotity _notify = new NormalNotity();
/// <summary>
/// 订阅商品
/// </summary>
/// <param name="aCommodity">商品</param>
public void Subscribe(Buyer buyer, Commodity aCommodity)
{
if (_ref.ContainsKey(buyer))
{
CommodityCollection list = _ref[buyer];
list.Add(aCommodity);
}
else
{
CommodityCollection list = new CommodityCollection();
list.Add(aCommodity);
_ref.Add(buyer, list);
}
aCommodity.ChangePrice += new ChangePriceEventHandler(aCommodity_ChangePrice);
}
/// <summary>
/// 取消订阅商品
/// </summary>
/// <param name="aCommodity">商品</param>
public void CancelSubscribe(Buyer buyer, Commodity aCommodity)
{
if (_ref.ContainsKey(buyer))
{
CommodityCollection list = _ref[buyer];
list.Remove(aCommodity);
aCommodity.ChangePrice -= new ChangePriceEventHandler(aCommodity_ChangePrice);
}
}
void aCommodity_ChangePrice(object sender, EventArgs e)
{
Commodity aCommodity = sender as Commodity;
foreach (KeyValuePair<Buyer, CommodityCollection> item in _ref)
{
CommodityCollection list = item.Value;
foreach (Commodity commodity in list)
{
if (commodity == aCommodity) //这里可以重载操作符(比如通过商品名称判断是否相等);
{
_notify.SendNotity(item.Key, commodity);
}
}
}
}
}
/// <summary>
/// 如果是多种通知用户价格变更的方式
/// 则可以抽象此类,变为多种策略实现;,比如可以改为邮件通知;
/// </summary>
public class NormalNotity
{
/// <summary>
/// 发送通知
/// </summary>
/// <param name="buyer"></param>
/// <param name="aCommodity"></param>
public void SendNotity(Buyer buyer, Commodity aCommodity)
{
Console.WriteLine(string.Format("用户[{0}] 获取 {1} 的最新价格为:{2}", buyer.ID, aCommodity.Name, aCommodity.Price.ToString()));
}
}
}
|