加入了一些自己的想法读慎,利用單例模式的思維對檢查類進(jìn)行靜態(tài)化,節(jié)省了內(nèi)存空間峡钓;利用中介者模式的思想妓笙,將鏈條的先后順序放在“中介者”里定義,方便修改能岩;
///
/// 檢查平臺寞宫,控制鏈執(zhí)行的順序
///
public class GoodsSale
{
private HandlerManager _handler = new HandlerManager();
private static SaleSecurityCheck _secuCheck = new SaleSecurityCheck();
private static SaleDataCheck _dataCheck = new SaleDataCheck();
private static SaleManager _manager = new SaleManager();
public bool Sale(string user, string customer, SaleModel model)
{
//1.檢查權(quán)限
_handler.Successor = _secuCheck;
if (!_handler.sale(user, customer, model))
{
return false;
}
//2.檢查數(shù)據(jù)
_handler.Successor = _dataCheck;
if (!_handler.sale(user, customer, model))
{
return false;
}
//3.確認(rèn)收貨
_handler.Successor = _manager;
if (!_handler.sale(user, customer, model))
{
return false;
}
return true;
}
}
///
/// 商品模型(實(shí)例用)
///
public class SaleModel
{
private string _goods;
private int _saleNum = 0;
public string Goods
{
get { return _goods; }
}
public int SaleNum
{
get { return _saleNum; }
}
public SaleModel(string goods, int num)
{
_goods = goods;
_saleNum = num;
}
public override string ToString()
{
string res = "none";
if (!string.IsNullOrEmpty(_goods))
{
res = "GoodsName: " + _goods + " Num: " + _saleNum;
}
return res;
}
}
///
/// 配置職責(zé)鏈的調(diào)用者
///
public class HandlerManager
{
private static SaleHandler _successor = null;
public SaleHandler Successor
{
get { return _successor; }
set { _successor = value; }
}
public bool sale(string user, string customer, SaleModel model)
{
return _successor.sale(user, customer, model);
}
}
///
/// 定義接口
///
public interface SaleHandler
{
bool sale(string user, string customer, SaleModel model);
}
///
/// 安全檢查
///
public class SaleSecurityCheck : SaleHandler
{
public bool sale(string user, string customer, SaleModel model)
{
if (user.Equals("ycj"))
{
Console.WriteLine("pass security check");
return true;
}
return false;
}
}
///
/// 數(shù)據(jù)校驗(yàn)
///
public class SaleDataCheck : SaleHandler
{
public bool sale(string user, string customer, SaleModel model)
{
#region check
if (string.IsNullOrEmpty(user))
{
Console.WriteLine("none user");
return false;
}
if (string.IsNullOrEmpty(customer))
{
Console.WriteLine("none customer");
return false;
}
if (model == null)
{
Console.WriteLine("wrong model");
return false;
}
if (string.IsNullOrEmpty(model.Goods))
{
Console.WriteLine("none goods");
return false;
}
if (model.SaleNum <= 0)
{
Console.WriteLine("wrong salenum");
return false;
}
#endregion
Console.WriteLine("pass data check");
return true;
}
}
///
/// 執(zhí)行收貨
///
public class SaleManager : SaleHandler
{
public bool sale(string user, string customer, SaleModel model)
{
Console.WriteLine("user: {0}, customer: {1}", user, customer);
Console.WriteLine(model.ToString());
return true;
}
}
static void Main(string[] args)
{
SaleModel model = new SaleModel("test", 10);
GoodsSale sale = new GoodsSale();
if (sale.Sale("ycj", "lhq", model))
{
Console.WriteLine("ok");
}
else
{
Console.WriteLine("no");
}
Console.ReadKey();
}