亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

petshop 4.0 中 BLL項目 中 接口的使用

petshop 4.0 中 BLL項目 中 接口的使用

茅侃侃 2018-10-25 11:10:17
public class Order {...private static readonly PetShop.IBLLStrategy.IOrderStrategy orderInsertStrategy = LoadInsertStrategy();... private static PetShop.IBLLStrategy.IOrderStrategy LoadInsertStrategy() {             // Look up which strategy to use from config file            string path = ConfigurationManager.AppSettings["OrderStrategyAssembly"];            string className = ConfigurationManager.AppSettings["OrderStrategyClass"];             // Using the evidence given in the config file load the appropriate assembly and class            return (PetShop.IBLLStrategy.IOrderStrategy)Assembly.Load(path).CreateInstance(className);}
查看完整描述

2 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

使用接口的意義就在于, 使用方(或者說客戶類)依賴于抽象而不是某個具體的類,這樣的代碼就會比較穩定,不會因為處理訂單的邏輯有變化,增加一個新的類了,就需要修改調用方的代碼。

舉個例子 ,假如使用的是某個具體類,那么客戶類(客戶類有可能是很多個,不止一個)的代碼是這樣子的:

private static readonly OrderStrategy orderStategy = LoadInsertStrategy();

這時,由于業務的需要,不得不增加了一個OrderStrategy2,那么涉及到這次修改的一部分客戶類,就需要修改代碼:

private static readonly OrderStrategy2 orderStategy = LoadInsertStrategy();

實際上,用接口的話,客戶類就不需要做修改了,因為可以動態地決定使用哪個策略類來實現需要的邏輯。

另外,這個也是GOF中的策略模式。



查看完整回答
反對 回復 2018-10-31
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

使用多態,

這樣可以有不同數據庫的數據訪問層,

他們都繼承接口,

可以使用反射技術來決定使用哪個數據訪問層,

也就決定使用了,哪種數據庫~~~


查看完整回答
反對 回復 2018-10-31
  • 2 回答
  • 0 關注
  • 344 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號