您好,登錄后才能下訂單哦!
小編給大家分享一下C#策略模式的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
所謂策略其實(shí)就是做一件事情有很多很多的方法,比如說(shuō)一個(gè)商場(chǎng)要搞促銷(xiāo),促銷(xiāo)的方式有可能有很多:打折啊,滿(mǎn)100返50啊、積分等等之類(lèi)的。這種不同的促銷(xiāo)方式在我們系統(tǒng)中表示就是一個(gè)一個(gè)的策略,并且策略是可以隨時(shí)更換的,這個(gè)時(shí)候在設(shè)計(jì)系統(tǒng)時(shí)就可以使用策略模式。
商場(chǎng)有可能會(huì)更換或追加新的促銷(xiāo)模式,也就是策略存在調(diào)整,也就是會(huì)更改以前的代碼,為了滿(mǎn)足開(kāi)閉原則,這時(shí)就要使用抽象類(lèi)和接口,這里我們偏向使用接口。在接口里面定義策略的方法,根據(jù)不同的情況編寫(xiě)不同的實(shí)現(xiàn)類(lèi),實(shí)現(xiàn)不同的策略,策略模式比較適用于算法經(jīng)常變化的情況,比如計(jì)算工資的方式、出行方式的選擇等等。
如圖所示,我們先定義策略的接口(Promotion),然后在這個(gè)策略接口里定義策略的方法(GetPrice()),接著我們定義了兩種具體的策略(Discount打折)和(MoneyBack返現(xiàn))。
策略模式會(huì)專(zhuān)門(mén)有一個(gè)上下文對(duì)象(PromotionContext)專(zhuān)門(mén)管理策略類(lèi),并且上下文對(duì)象和策略接口之間是聚合的關(guān)系,也就是整體和部分的關(guān)系,因此在上下文對(duì)象里應(yīng)該保存一個(gè)促銷(xiāo)類(lèi)型的引用,另外上下文對(duì)象里一般會(huì)有一些方便客戶(hù)端調(diào)用的方法,如GetPrice()??蛻?hù)端程序可以通過(guò)上下文對(duì)象得到價(jià)格,這個(gè)GetPrice()里會(huì)根據(jù)不同的策略,執(zhí)行不同的策略方法。
如果客戶(hù)端不想使用上下文中定義的默認(rèn)的策略,也可以去修改策略類(lèi),因?yàn)樯舷挛闹杏幸粋€(gè)ChangePromotion()的方法,客戶(hù)端主要使用上下文對(duì)象,如果需要修改策略,他還要依賴(lài)于具體的策略對(duì)象。
1、策略接口:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /* 策略接口 */ public interface IPromotion { /// <summary> /// 根據(jù)原價(jià)和策略計(jì)算新價(jià)格 /// </summary> /// <param name="originPrice">原價(jià)</param> /// <returns></returns> double GetPrice(double originPrice); } }
2、Discount打折策略類(lèi)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /// <summary> /// 打折策略類(lèi) /// </summary> public class Discount :IPromotion { public double GetPrice(double originPrice) { Console.WriteLine("打八折:"); return originPrice * 0.8; } } }
3、MoneyBack返現(xiàn)類(lèi)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /* 返現(xiàn)策略類(lèi):滿(mǎn)100返50的策略 */ class MoneyBack :IPromotion { public double GetPrice(double originPrice) { Console.WriteLine("滿(mǎn)100返50"); return originPrice - (int)originPrice / 100 * 50; } } }
4、策略上下文類(lèi)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { /* 策略上下文,為客戶(hù)選擇合適的策略 */ public class PromotionContext { private IPromotion p = null; public PromotionContext(IPromotion p) { this.p = p; } public double GetPrice(double originPrice) { // 默認(rèn)策略 if (this.p == null) { this.p = new Discount(); } return this.p.GetPrice(originPrice); } /// <summary> /// 更改策略的方法 /// </summary> /// <param name="p"></param> public void ChangePromotion(IPromotion p) { this.p = p; } } }
5、主程序調(diào)用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 策略模式 { class Program { static void Main(string[] args) { // 默認(rèn)策略:打八折的策略 PromotionContext pc = new PromotionContext(null); Console.WriteLine(pc.GetPrice(200)) ; // 更改策略:滿(mǎn)100返50的策略 pc.ChangePromotion(new MoneyBack()); Console.WriteLine(pc.GetPrice(155.9)); Console.ReadKey(); } } }
以上是“C#策略模式的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。